Q:

Create temporary file in specified directory in Java

belongs to collection: Java Files & Directories Programs

0

Package to use: import java.io.*;

Syntax:

    // file object creation with directory name (path)
    File tempFile = new File("E://Java");

    // creating temporary file in specified directory
    File.createTempFile("JavaTemp", ".javatemp", tempFile);

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Create a directory in Java... >>
<< Create a temporary file in Java...