How To Create New Text File

FileIONavigationExamplepackage fileionavigationexample;
import java.io.*;
public class ToCeateNewTextFile {
public static void main(String[] args) {
boolean f = false;
File file = new File(“C:\Users\KUNDAN\Documents\First.txt”);//Its just creating the name of file but not actual file.
//to create actual file we need the below code after the declaring name of file.
System.out.println(file.exists());//first time compiling the code gives “false” but 2nd time gives “true.”
try{
f = file.createNewFile(); //after compiling this code you will see an empty text file as “First.txt” at the specified address.
System.out.println(f); //At very first time it will return true but second time it returns false b/c once file created than
//again it will not create the same file.
System.out.println(file.exists());//all time return “true”.
}catch(IOException e){
e.printStackTrace();
}
}

}

/*initial output :
———————-
false
true
true

after initial output:
———————-
true
false
true */

One thought on “How To Create New Text File”

Leave a Reply