Site icon

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)

public static File createTempFile(String prefix, String suffix, File directory) 
throws IOException

2.) Creates a temporary file under temp directory

public static File createTempFile(String prefix, String suffix) throws IOException

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

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
Exit mobile version