Java code to create temporary file in specified directory
// Java code to create temporary file
// in specified directory
import java.io.*;
public class CreateTempFileinDIR {
public static void main(String[] args) {
// file object to store the result
File result = null;
// file object creation with directory
// name (path)
File tempFile = new File("E://Java");
try {
// creating temporary file in specified directory
result = File.createTempFile("JavaTemp", ".javatemp", tempFile);
} catch (IOException exp) {
System.out.println("Error in creating temporary file : " + exp);
}
// printing the created file path
System.out.println("Temporary file created at : " + tempFile.getPath());
}
}
Output
E:\Programs>javac CreateTempFileinDIR.java
E:\Programs>java CreateTempFileinDIR
Temporary file created at : E:\Java
Java code to create temporary file in specified directory
Output