Java Examples - Creation of File

Advertisements


Problem Description:

How to create a file in a specified directory ?

Solution:

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

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());
   }
}

Result:

The above code sample will produce the following result.

C:\JavaTemp37056.javatemp


Advertisements
Advertisements