How to create a new file using java code

New test file can be created using the below java code.
import java.io.File;
import java.io.IOException;

public class CreateFile {
   public static void main(String[] args) {
      try{
         File file = new File("C:/newfile.txt");
         if(file.createNewFile())
         System.out.println("File created Successfully!");
         else
         System.out.println
         ("Error, file may already exists.");
     }
     catch(IOException ioe) {
        ioe.printStackTrace();
     }
   }
}
Output: File created Successfully!

No comments:

Post a Comment