Create temporary file in a temp or input directory – java (example)

  1. Create a temporary file in java.
  2. We can create temporary file by specifying prefix, suffix (or directory)
  3. We will demonstrate following features.
    • Create temporary file in temp directory (default location).
    • Create temporary file in a directory specified by user.
    • Create temporary file without specifying “suffix” (or file extension).
      • We will get default file extension of tmp (e.g. blabla.tmp)
    • Create temporary file by specifying the extension like “log”, “txt”, “mp4” etc.
  4. Java SDK provides couple of methods to create temporary (or temp) file.

1.) Creates a temporary file in specified directory (java/example)

  • Create file by providing prefix, suffix and destination directory.
  • In program, we will create a temporary file in current working directory.
public static File createTempFile(String prefix, String suffix, File directory) 
throws IOException
  • Length of prefix should be more the 3.
  • If suffix is null then it defaults to .tmp (file extension of file)
  • If directory is null then directory path of temp file is dependent on property “java.io.tmpdir”
    • Refer below code : System.getProperty(“java.io.tmpdir”)
  • If directory is specified, temporary file will be created in specified directory.

2.) Creates a temporary file under temp directory

  • Create temp file by providing the prefix (or file name) and suffix (or file extension).
  • In program, the file will be created in temporary directory.
public static File createTempFile(String prefix, String suffix) throws IOException
  • createTempFile internally calls createTempFile(String prefix, String suffix, null) only.

We have shown the temporary file creation process in Fig 1.

  • User provide prefix, suffix and input directory path then temp file will be created in input directory.
  • User provide prefix and suffix for temp file then file will be created in temp directory (default location).
temporary file folder directory java example
Fig 1: Temp file with/without directory

3. Program: create temporary file in temp/input directory (java/example)

package org.learn;

import java.io.File;
import java.io.IOException;

public class TempFileExamples {
	public static void main(String[] args) {
		try {
			String tempDirectoryPath = System.getProperty("java.io.tmpdir");
			System.out.println("1. Default temp directory path: " + tempDirectoryPath);
			// path of temp directory on windows directory
			// output: C:\Users\sony\AppData\Local\Temp\

			// Create temp file using prefix and suffix
			File tempFile = File.createTempFile("myPrefix", null);
			System.out.println("2. With prefix and no suffix :" + tempFile.getAbsolutePath());
			// output:
			// C:\Users\sony\AppData\Local\Temp\myPrefix77153538732490557.tmp

			// Create temp file using prefix and suffix
			tempFile = File.createTempFile("myPrefix", ".ext");
			System.out.println("3. With prefix and suffix :" + tempFile.getAbsolutePath());
			// output:
			// C:\Users\sony\AppData\Local\Temp\myPrefix1955355708831001975.ext

			// Create temp file using prefix, suffix and null directory
			tempFile = File.createTempFile("myPrefix", ".ext", null);
			System.out.println("4. With prefix, suffix and no directory:" + tempFile.getAbsolutePath());
			// output:
			// C:\Users\sony\AppData\Local\Temp\myPrefix1955355708831001975.ext

			String currentDirectory = System.getProperty("user.dir");
			// Create temp file using prefix, suffix and "D:/" directory
			tempFile = File.createTempFile("myPrefix", ".ext", new File(currentDirectory));
			System.out.println("5. Prefix, suffix and current directory: " + tempFile.getAbsolutePath());
			// output: D:\Code\myPrefix3024444275963606033.ext

		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}
}

4. Output:create temporary file in temp/input directory (java/example)

1. Default temp directory path: C:\Users\admin\AppData\Local\Temp\
2. With prefix and no suffix :C:\Users\admin\AppData\Local\Temp\myPrefix6375174484136497040.tmp
3. With prefix and suffix :C:\Users\admin\AppData\Local\Temp\myPrefix7704027585818607103.ext
4. With prefix, suffix and no directory:C:\Users\admin\AppData\Local\Temp\myPrefix3528167598022753430.ext
5. Prefix, suffix and current directory: D:\Code\myPrefix3024444275963606033.ext
Scroll to Top