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.
DeclarationFollowing is the declaration for java.io.File.createNewFile() method −
public boolean createNewFile()Parameters
NA
Return ValueThis method returns true, if the named file does not exist and was successfully created. The method returns false if the file exists.
ExceptionIOException − If an I/O error occurs
SecurityException − If SecurityManager.checkWrite(java.lang.String) method denies write access to the file
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.
FileDemo.javapackage 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("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: trueExample - Usage of File createNewFile() method
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.
FileDemo.javapackage 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("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: trueExample - Usage of File createNewFile() method
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.
FileDemo.javapackage 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_io_file_methods.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4