How to create temporary file in Java

Here’s an example to create a temporary file in Java.

Example


package com.mkyong.file;

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

public class CreateTempFileExample
{
    public static void main(String[] args)
    {	
    
    	try{
    		
    	   //create a temp file
    	   File temp = File.createTempFile("temp-file-name", ".tmp"); 
    		
    	   System.out.println("Temp file : " + temp.getAbsolutePath());
    		
    	}catch(IOException e){
    		
    	   e.printStackTrace();
    		
    	}
    	
    }
}

Result


Temp file : C:\Users\mkyong\AppData\Local\Temp\temp-file-name623426.tmp

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
DanielGuestMake a temporary file in Java | KarstieltopiaEdward Recent comment authors
newest oldest most voted
Guest
Guest
Guest

if I was surprised by first statement
I was dead by second statement

Daniel
Guest
Daniel
trackback
Make a temporary file in Java | Karstieltopia
Edward
Guest
Edward

Cool! Don’t know about this feature of File class.