Create File Java Program

Sharing is healthy !!

Today we are going to learn create file java program.

Here we will be using two classes, that is, file class and IOExceptionclass of java inputoutput package.

Then in new File instance we will be converting the given pathname string into an abstract pathname.

The file class instance method, createNewFile(), creates new file if not created. Let’s take a look on program below,

Example: Create File Java Program

Create File Java Program

Also Read – Create Directory Java Program

import java.io.File;
import java.io.IOException;

public class CreateFile
{
   public static void main(String[] args)
   {
      try
      {
         File fl = new File("C:/createfile.txt");
         if(fl.createNewFile())
         {
            System.out.println("File created");
         }
         else
         {
            System.out.println("File exists");
         }
      }
      catch(IOException ioe)
      {
         ioe.printStackTrace();
      }
   }
}

Output:

File created

Sharing is healthy !!

Leave a Reply

Your email address will not be published.