Create Temporary File in Specified Directory in Java

You are currently viewing Create Temporary File in Specified Directory in Java

Create temporary file in specified directory in Java

// 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

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply