What is FileUtils.getTempDirectory() in Java?

getTempDirectory() is a staticthe methods in Java that can be called without creating an object of the class. method of the FileUtils class that is used to obtain an instance of the File class pointing to the system temporary directory.

Refer to this answer to get the absolute path to the system temporary directory.

How to import FileUtils

The definition of FileUtils can be found in the Apache Commons IO package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
</dependency>

For other versions of the commons-io package, refer to the Maven Repository.

You can import the FileUtils class as follows:


import org.apache.commons.io.FileUtils;

Syntax


public static File getTempDirectory()

Parameters

The method has no parameters.

Return value

This method returns a File object pointing to the system temporary directory.

Code

import org.apache.commons.io.FileUtils;
import java.io.File;
public class Main{
public static void main(String[] args){
File tmpFile = FileUtils.getTempDirectory();
System.out.println("The path to the system temporary directory is " + tmpFile.getAbsolutePath());
}
}

In the code above, we get the File instance that points to the system temporary directory with the help of the FileUtils.getTempDirectory() method.

Then, we print the absolute path of the system temporary directory using the instance obtained through the getAbsolutePath() method.

Output

The output of the code will be as follows:


The path to the system temporary directory is /var/folders/dt/blzdgmcs3vq7hl7ftp997r8w0000gn/T

Free Resources

Attributions:
  1. undefined by undefined