What is FileUtils.getTempDirectoryPath() in Java?

Overview

getTempDirectoryPath() is a staticthe methods in Java that can be called without creating an object of the class method of FileUtils that is used to return the path to the system’s temporary directory.

The path to the system’s temporary directory is stored in the system property variable, java.io.tmpdir.

How to import FileUtils

The definition of FileUtils 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 FileUtils class as follows:

import org.apache.commons.io.FileUtils;

Syntax

public static String getTempDirectoryPath()

Parameters

The method has no parameters.

Return value

The getTempDirectoryPath method returns the path to the system’s temporary directory.

Code

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

Explanation

In the above code, we print the temporary directory path that we obtain from the FileUtils.getTempDirectoryPath() method.

Output

The output of the code is as follows:

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

The output might be different when this code is run on different machines.

Free Resources