How to create temporary file in Java 1.7 with java.nio package.

In this tutorial, let us see how to create temporary file in java. In many application, it is required to create a temporary file that needs to be used one time only. Temporary fie can be created in java 1.5 using java.nio package in the default temporary folder or any specific folder. Temporary file creation does not require any file name. Random number generated by Java is used as file name.

To create temporary file, the following two methods are used.
1. createTempFile(Path, String, String, FileAttribute<?>… attrs) – Creates tmp file in the specified directory.

The above method accepts four arguments.

Path – > to specify the directory in which the file to be created.

String -> to mention prefix of the filename. Use null to avoid prefix.

String -> to mention suffix of the file name. i.e. file extension. Use null to use .tmp as extension.

attrs -> This is optional to mention list of file attributes to set atomically when creating the file


Files.createTempFile(path,null, null); – creates a temp file with .tmp extension in the specified path.

Files.createTempFile(path,”MyFile”, “.txt); – creates a temp file with the given prefix and suffix similar to MyFile8131801983650433596.txt

2. createTempFile(String, String, FileAttribute<?>) – Creates tmp file in the default temporary directory of the system / server.

Files.createTempFile(null,null) – creates a temp file in the default temp folder of the system. In windows, temp folder may be C:\Users\username\AppData\Local\Temp
where username is your windows login id.

The following program creates a temporary file at temp folder under working directory, write and read content. Please ensure that you have created temp folder inside working directory.

package com.javaonline;

import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class JavaTempFile {

	public static void main(String args[])

		String fileContent="Java temporary file can be created in java using java.nio package.";

	Path tempFile =null;

		 String workingDir = System.getProperty("user.dir");
		 Path path = FileSystems.getDefault().getPath(workingDir+"/temp","");

		try {

		    // tempFile =  Files.createTempFile(path,null, null);
			 tempFile =  Files.createTempFile(path,"MyFile", ".txt");
		      Files.write(tempFile, fileContent.getBytes());
		     System.out.println("Temporary file  created "+  tempFile);

		 	Charset charset = Charset.forName("US-ASCII");

				BufferedReader reader = Files.newBufferedReader(tempFile, charset);
			    String line = null;
			    while ((line = reader.readLine()) != null) {


		} catch (IOException x) {
		    System.err.format("IOException", x);


Running the above program will gives the below output..

Temporary file created F:\Eclipse\JavaApps\temp\MyFile3757366232421138719.txt
Java temporary file can be created in java using java.nio package.

where F:\Eclipse\JavaApps\ is the working directory of eclipse.



Reference :

Leave a Reply