Java - File createNewFile() Method



Description

The Java File createNewFile() method atomically creates a new file named by this abstract path name. FileLock facility should be used instead of this method for file-locking as the resulting protocol cannot be made to work reliably.

Declaration

Following is the declaration for java.io.File.createNewFile() method −

public boolean createNewFile()

Parameters

NA

Return Value

This method returns true, if the named file does not exist and was successfully created. The method returns false if the file exists.

Exception

  • IOException − If an I/O error occurs

  • SecurityException − If SecurityManager.checkWrite(java.lang.String) method denies write access to the file

Example 1

The following example shows the usage of Java File createNewFile() method. We've created a File reference. Then we're creating a File Objects using test.txt which is not present in the system. Using createNewFile() method, we're creating the file and printing the result. Now using delete() method, we're deleting the file and then again try to create the file and print the result.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      boolean bool = false;
      
      try {
         // create new file
         f = new File("F://test.txt");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // prints
         System.out.println("File created: "+bool);
         
         // deletes file from the system
         f.delete();
         
         // delete() is invoked
         System.out.println("delete() method is invoked");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // print
         System.out.println("File created: "+bool);
            
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

File created: true
delete() method is invoked
File created: true

Example 2

The following example shows the usage of Java File createNewFile() method. We've created a File reference. Then we're creating a File Objects using test.txt which is now present in the system. Using createNewFile() method, we're creating the file and printing the result. Now using delete() method, we're deleting the file and then again try to create the file and print the result.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      boolean bool = false;
      
      try {
         // create new file
         f = new File("F://test.txt");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // prints
         System.out.println("File created: "+bool);
         
         // deletes file from the system
         f.delete();
         
         // delete() is invoked
         System.out.println("delete() method is invoked");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // print
         System.out.println("File created: "+bool);
            
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

File created: false
delete() method is invoked
File created: true

Example 3

The following example shows the usage of Java File createNewFile() method. We've created a File reference. Then we're creating a File Objects using test.txt which cannot be created being in restricted location. Using createNewFile() method, we're creating the file and printing the result. Now using delete() method, we're deleting the file and then again try to create the file and print the result.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      boolean bool = false;
      
      try {
         // create new file
         f = new File("C://Windows/test.txt");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // prints
         System.out.println("File created: "+bool);
         
         // deletes file from the system
         f.delete();
         
         // delete() is invoked
         System.out.println("delete() method is invoked");
         
         // tries to create new file in the system
         bool = f.createNewFile();
         
         // print
         System.out.println("File created: "+bool);
            
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

java.io.IOException: Access is denied
	at java.base/java.io.WinNTFileSystem.createFileExclusively(Native Method)
	at java.base/java.io.File.createNewFile(File.java:1035)
	at FileDemo.main(FileDemo.java:13)
java_file_class.htm
Advertisements