java

How to create a temporary file in Java

In this tutorial, we are going to see how to create a temporary file in Java by using the File.createTempFile() method.
 

Java Program to create a temporary file:
import java.io.*;

public class Main
{
    public static void main(String[] args)
    {	
    	try{
    	   //Create a temporary file
    	   File tmp = File.createTempFile("myTempFile", ".tmp"); 
    		
    	   System.out.println("Temporary file : " + tmp.getAbsolutePath());
    		
    	}catch(IOException e){	
    	   e.printStackTrace();
    	}
    }
}

Output:

Temporary file : C:\users\StackHowTo\AppData\Local\Temp\myTempFile21354786986.tmp
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *