How to create a text file in java

In this problem we learn how to create a text file in Java. This file will be empty and created if their is no file with that name already exist. If file exist already the error message will be display else program execute successfully with the message your file is created.

Source Code


package www.ancodingpoint.com;
import java.io.File;

public class Creating_File {

  public static void main(String[] args) {
      try {
          File f = new File (" ancodingpoint.txt ");
          if (f.createNewFile())
              System.out.println(" Your File is Created! ");
          else 
          System.out.print(" File Already Exist  ");
      }
               catch (Exception e)
              {
        System.out.println(" Error found ");
    }
  }
}

Output of the Program  


Java Filing Programming

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment