Subscribe to Get Free Material Updates!
Visit my new blog WebData Scraping - Web Scraping Service provider company in India.

Java Program To Create New File


The below Java program creates new blank file named MyFirstFile.txt by passing  file name as argument to the constructor of File object. We must have to place file creation code in try block because it may throw IOException. We can check whether new file is created or not by using createNewFile() method of File object and place it in if...else condition.

import java.io.*;
class CreateFile
{
  
 public static void main(String[] args) 
 {
  
  try
  {
   /*Creates File MyFirstFile.txt in current Directory*/
   File f1=new File("MyFirstFile.txt"); 
   
   if(f1.createNewFile())
    System.out.println("File Created Successfuly!");
   else
    System.out.println("Fail to Create File!");
  
  }

  catch(IOException e)
  {

  }

 }
}

1 comments:

javin paul said...

Nice article man, you have indeed coveted the topic well. you can also check this file and directory tutorial in java for some more tips.

Post a Comment

Related Posts Plugin for WordPress, Blogger...

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes