How to create a file in a specified directory ?


Levels of difficulty: / perform operation:

Problem & Solution

This example demonstrates how to create a file in a specified directory using File.createTempFile() method of File class.

JAVA Program

import java.io.File;

public class Main {
   public static void main(String[] args) 
   throws Exception {
      File file = null;
      File dir = new File("C:/");
      file = File.createTempFile
      ("JavaTemp", ".javatemp", dir);
      System.out.println(file.getPath());
   }
}


Output

The above code sample will produce the following result

C:\JavaTemp38756.javatemp