Programming Tips - Java: Get the system temporary folder

Date: 2019mar21 Update: 2025sep26 Language: Java Keywords: TEMP, tmp Q. Java: Get the system temporary folder A. Do System.getProperty("java.io.tmpdir") as shown in this full example:
class Demo { // Helper functions private static boolean isWindows() { return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0; } private static String removeTrailingSlash(final String s) { return s.replaceFirst("[/\\\\]$", ""); } private static String toForwardSlashes(final String s) { return s.replace("\\", "/"); } public static String systemTempDir() { String dir = System.getProperty("java.io.tmpdir"); if (isWindows()) { dir = removeTrailingSlash(dir); dir = toForwardSlashes(dir); } return dir; } public static final void main(String []args) { System.out.println("dir=" + systemTempDir()); } }
Output (on my Linux box):
dir=/tmp
If you just want a tmp file use File.createTempFile()