Java - Create temporary file example

Posted on November 3, 2016


The createTempFile () method of the File class is used to create temporary file in java. The createTempFile () method return newly created temporary empty file.

Here is an example to demonstrate this.

CreateTempFileExample.java

package com.boraji.tutorial.io;

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

/**
 * @author imssbora
 * CreateTempFileExample.java
 * Nov 3, 2016
 */
public class CreateTempFileExample {
	public static void main(String[] args) {
		try {
			File file=File.createTempFile("BORAJI", ".tmp");
			
			if(file.exists()){
				System.out.println("Temporary File Path: "+file.getAbsolutePath());	
			}
		} catch (IOException e) {
			// Temp file creation failed
			e.printStackTrace();
		}
		
	}
}

Output

Temporary File Path: C:\Users\SUNILS~1\AppData\Local\Temp\BORAJI500834714653637902.tmp