File class createNewFile()method example
creates a new, empty file named by this abstract pathname
File class createNewFile()method example. This example shows you how to use createNewFile()method.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.
Here is the code:-
/**
* @Program that Atomically creates a new, empty file named by this abstract
* pathname if and only if a file with this name does not yet exist.
* CreateNewFile.java
* Author:-RoseIndia Team
* Date:-30-May-2008
*/
import java.io.*;
public class CreateNewFile {
public static void main(String[] args) throws IOException {
// Create a File object
File myFile = new File("/home/girish/Desktop/g2.txt");
// Tests whether the file or directory denoted by this abstract
//pathname exists.
if (myFile.exists()) {
System.out.println(myFile.getAbsolutePath() +
" File already exists");
} else {
//creates a new, empty file named by this pathname
myFile.createNewFile();
System.out.println(myFile.getAbsolutePath() +
" File Created Successfully");
}
}
} |
Output of the program:-
| /home/girish/Desktop/g2.txt File Created Successfully |
|