EN
Java - find temporary directory path
11 points
In this short article, we would like to show how to find path to temporary directory where we can store temporary files using Java.
Quick solution:
xxxxxxxxxx
1
String path = System.getProperty("java.io.tmpdir");
By default, operating systems have a place to store temporary files needed by many applications.
Motivation to use that store may be special features like:
- protection against access across different users,
- supported fast IO operations (e.g. virtual file systems in memory or dedicated fast storage),
- cleaning when the operating system is turned off.
Note: in Java we can change default temporary directory path by running:
java -Djava.io.tmpdir=/path/to/temporary-directory
Example program:
xxxxxxxxxx
1
package com.example;
2
3
public class Program {
4
5
public static void main(String[] args) {
6
7
String path = System.getProperty("java.io.tmpdir");
8
System.out.println(path);
9
}
10
}
Example output (under Windows):
xxxxxxxxxx
1
C:\Users\john\AppData\Local\Temp\
Where: john
is directory used by currently logged-in user account.