Create File in Java

File is nothing but a simple storage of data in Java language. We call one object that belongs to java.io package and we use it to store name of the file or directory and also use the pathname. File is collection of stored information that are arranged in String, column and row line. This example stores file name and text data to the file. The File.createNewFile() method is used to create a file in java.

Ads

File is nothing but a simple storage of data in Java language. We call one object that belongs to java.io package and we use it to store name of the file or directory and also use the pathname. File is collection of stored information that are arranged in String, column and row line. This example stores file name and text data to the file.

The File.createNewFile() method is used to create a file in java. This method returns a Boolean value "true" if the file is created successfully, "false" if the File is not created.

NewFile() method returns "false" if the file is already created or the operation has failed.

Example of how to create a file in java:

package FileHandling;

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

public class FileCreate {
	public static void main(String[] args) throws IOException {
		File file = new File("c:/Test.txt");
		if(file.createNewFile()){
			System.out.println("File is created");
		}else
		System.out.println("File is already created");
	}

}

Output:

File is created

Advertisements

Share on Google+Share on Google+

Create File in Java

Posted on: May 6, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Create File in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads