Table of Contents
In this post, we will see how to get temp directory path in java.
Get Temp Directory Path in Java
Using System.getProperty()
To get the temp directory path, you can simply use System.getProperty("java.io.tmpdir")
. It will return default temporary folder based on your operating system.
Default temp directory path
Windows : %USER%\AppData\Local\Temp
Linux/Unix: /tmp
Let’s see with the help of example:
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog; public class GetTemporaryPathMain { public static void main(String[] args) { String tempDirPath = System.getProperty("java.io.tmpdir"); System.out.println("Temp directory path : " + tempDirPath); } } |
Output:
1 2 3 |
Temp directory path : C:\Users\Arpit\AppData\Local\Temp\ |
By Creating Temp File and Extracting Temp Path
We can also create temp file and extract temp directory path using String’s substring() method.
Using java.io.File
To get temp directory path:
- Use
File.createTempFile()
to create temp file. - Get absolute path using
File's getAbsolutePath()
method. - Use substring() method to extract temp directory path from absolute path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog; import java.io.File; import java.io.IOException; public class CreateFileAndGetTempDirMain { public static void main(String[] args) { try { // Create temp file File temp = File.createTempFile("temp_", ".tmp"); String absTempFilePath = temp.getAbsolutePath(); System.out.println("Temp file path : " + absTempFilePath); // Get temp directory path using substring method String tempDirPath = absTempFilePath .substring(0, absTempFilePath.lastIndexOf(File.separator)); System.out.println("Temp directory path : " + tempDirPath); } catch (IOException e) { e.printStackTrace(); } } } |
Output:
1 2 3 4 |
Temp file path : C:\Users\Arpit\AppData\Local\Temp\temp_10478314566038976912.tmp Temp directory path : C:\Users\Arpit\AppData\Local\Temp |
Using java.nio.File.Files
To get temp directory path:
- Use
Files.createTempFile()
to create temp file. - Get absolute path using
toString()
method. - Use substring() method to extract temp directory path from absolute path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; public class CreateFileNIOAndGetTempDirMain { public static void main(String[] args) { try { // Create temp file Path temp = Files.createTempFile("temp_", ".tmp"); String absTempFilePath = temp.toString(); System.out.println("Temp file path : " + absTempFilePath); String fileSeparator = FileSystems.getDefault().getSeparator(); String tempDirPath = absTempFilePath .substring(0, absTempFilePath.lastIndexOf(fileSeparator)); System.out.println("Temp directory path : " + tempDirPath); } catch (IOException e) { e.printStackTrace(); } } } |
Output:
1 2 3 4 |
Temp file path : C:\Users\Arpit\AppData\Local\Temp\temp_6905627814634776014.tmp Temp directory path : C:\Users\Arpit\AppData\Local\Temp |
Further reading:
Override Default Temp Directory Path
If you want to override temp directory path, you can run java program with JVM arguments as -Djava.io.tmpdir=Temo_file_path
For example:
If you want set temp directory path as C:\temp
, you can run java program with JVM argument as -Djava.io.tmpdir=C:\temp
That’s all about how to get temp directory path in Java.