Create new file in java

Ads
 

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.

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

Ads

Ads