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.

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.

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. 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