Share on Google+Share on Google+

Create File in Java

In this section, you will see how to create file in java.

Create a File 



Whenever the data is need to be stored, a file is used to store the data. File is a collection of stored information that are arranged in string, rows, columns and lines etc.
In this section, we will see how to create a file. This example takes the file name and text data for storing to the file.

For creating a new file File.createNewFile( ) method is used. This method returns a boolean value true if the file is created otherwise return false. If the mentioned file for the specified directory is already exist then the createNewFile() method returns the false otherwise the method creates the mentioned file and return true. 

Lets see an example that checks the existence of  a specified file.


public class CreateFile1{
  public static void main(String[] args) throws IOException{
  File f;
  f=new File("myfile.txt");
  System.out.println("New file \"myfile.txt\" has been created 
  to the current directory"

First, this program checks, the specified file "myfile.txt" is exist or not. if it does not exist then a new file is created with same name to the current location. 

Output of the Program


C:\nisha>java CreateFile1
New file "myfile.txt" has been created to the current directory


If you try to run this program again then after checking the existence of the file, it will not be created and you will see a message as shown in the output.


C:\nisha>java CreateFile1
The specified file is already exist


Download this Program




Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Create File in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 1, 2012
pokemon tower defense BW

love java
Mary Lois Dela Cruz
March 2, 2012

i love my all subject teachers and my classmate
March 19, 2012
can remove is exist checck

In above code, condition if(!f.exists()) is not required, since createNewFile() API creates new file only if file with name specified does not exist
June 29, 2012
Thank you!

Wow helped a lot :D thanks
September 20, 2012

file & streams in java
September 25, 2012
createNew FIle

it doesn't work.. It says, it needs exception
October 6, 2012

the above program doesn't return the statement as you mentioned above when it compile and run the second time
October 6, 2012

if we want to get the above 2nd result, we have to write a code like this, import*; public class CreateFile1{ public static void main(String[] args) throws IOException{ File f; f=new File("myfile.txt"); if(!f.exists()){ f.createNewFile(); System.out.println("New file \"myfile.txt\" has been created to the current directory"); } else System.out.println("\n the file is already exists"); } }
October 14, 2012