- Java.io package classes
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package extras
- Java.io - Interfaces
- Java.io - Exceptions
- Java.io package Useful Resources
- Java.io - Discussion
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)
To Continue Learning Please Login
Login with Google