Best way to get system temporary directory in Java


Best and safe way to get users temp directory in Java. This can be done using FileUtils class of Apache Commons IO. Commons IO is well tested library. The Commons IO library contains utility classes, stream implementations, file filters, file comparators, endian transformation classes, and much more.

Platform information

jdk1.8.0_60, Java 8, Eclipse

Library Information

Used library Name:

Commons IO

Maven Dependency Entry

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

Gradle Dependency Entry

compile \'commons-io:commons-io:2.4\'

Jar Download Location

Download Jar

Sample code to get  system temporary directory

package org.code4copy;

import org.apache.commons.io.FileUtils;

public class TempDirectory {

public static String getTempDir() {
return FileUtils.getTempDirectoryPath();
}

public static void main(String[] args) {
System.out.println("System\'s Temp Location: " + getTempDir());
}
}

Output of program

System\'s Temp Location: C:UsersbimbiAppDataLocalTemp

Download code from here


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.