What is SystemUtils.getJavaIoTmpDir() in Java?
getJavaIoTmpDir() is a SystemUtils class which is used to return the temporary directory used by Java for input/output operations as an instance of the File class. The path to the Java IO temporary directory is stored as a system property under the name java.io.tmpdir.
How to import SystemUtils
The definition of SystemUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the commons-lang package, refer to the Maven Repository.
You can import the SystemUtils class as follows:
import org.apache.commons.lang3.SystemUtils;
Syntax
public static File getJavaIoTmpDir()
Parameters
The method accepts no parameters.
Return value
This method returns the File object pointing to the Java IO temporary directory.
Code
import org.apache.commons.lang3.SystemUtils;import java.io.File;public class Main{public static void main(String[] args){File javaIoTmpDir = SystemUtils.getJavaIoTmpDir();System.out.printf("The absolute path of the java io temp directory is '%s'.", javaIoTmpDir.getAbsolutePath());}}
Output
The output of the code will be as follows:
The absolute path of the java io temp directory is '/var/folders/dt/blzdgmcs3vq7hl7ftp997r8w0000gn/T'.
Explanation
Running the code above in your system can give a different output depending on your machine.
In the code above, we are using the getJavaIoTmpDir() method to get the File object pointing to the temporary directory that java uses for IO operations.
Next, we print the absolute path of the temporary directory used for input/output by Java.