Home Java Beginners Create new file in java

 
 

Share on Google+Share on Google+

Create new file in java

Advertisement
We are going to discuss how to create new file in java. First of all we have create class "CreateFile".and after that we create main method than we use try and catch block. We have created File object in java File class representing the files and folder pathnames by location.

Create new file in java

We are going to discuss how to create new file in java. First of all we have create class "CreateFile".and after that we create main method than we use try and catch block. We have created File object in java File class representing the files and folder pathnames by location. With File class we create files and directories. Java File represents actual file location in a System. We have created a new file fl.createNewFile() method. This method returns a Boolean value true if file is already created and returns false and display a message if File is already created.

You can see below example

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

public class CreateFile {
	public static void main(String[] t) {
		File fl = new File("c:/Rose.txt");
		try {
			if (fl.createNewFile()) {
				System.out.println("file is successfully created");
			} else {
				System.out.println("File is already create");
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}
}

OutPut:-

file is successfully created

File is already created

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: July 29, 2013

Related Tutorials

Discuss: Create new file in java  

Post your Comment


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