Get Operating System temporary directory / folder in Java


To get the temporary directory in Java, use the System.getProperty() method. Use the

It’s syntax is −

String getProperty(String key)

Above, the key is the name of the system property. Since, we want the Java Home Directory name, therefore we will add the key as −

java.io.tmpdir

The following is an example −

Example

 Live Demo

public class Demo {
   public static void main(String []args){
      String strTmp = System.getProperty("java.io.tmpdir");
      System.out.println("OS current temporary directory: " + strTmp);
      System.out.println("OS Name: " + System.getProperty("os.name"));
      System.out.println("OS Version: " + System.getProperty("os.version"));
   }
}

Output

OS current temporary directory: /tmp
OS Name: Linux
OS Version: 3.10.0-862.9.1.el7.x86_64

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

768 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements