createNewFile(): This method is available in the java.io.File class of Java.
Syntax:
boolean java.io.File.createNewFile() throws IOException
This method atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other file system activities that might affect the file.
Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably.
Parameters: NA
Returns: true if the named file does not exist and was successfully created; false if the named file already exists.
Throws:
1. IOException - If an I/O error occurred.
2. SecurityException - If a security manager exists and its java.lang.SecurityManager.checkWrite(java.lang.String) method denies write access to the file.
Approach
Java
import java.io.File;import java.io.IOException;public class FilecreateNewFile {public static void main(String[] args) {String pathname = "D:\\hello.txt";File file = new File(pathname);try {System.out.println(file.createNewFile());} catch (IOException e) {System.out.println("Error while creating");}}}
Output:
true
No comments:
Post a Comment