Open In App

Java Program to Create a New File

Last Updated : 10 Jan, 2025
Suggest changes
Share
Like Article
Like
Report

There are two standard methods to create a new file, either directly with the help of the File class or indirectly with the help of the FileOutputStream class by creating an object of the file in both approaches.

Methods to Create Files in Java

There are two methods mentioned below

  • Using the File Class
  • Using the FileOutputStream Class

Both classes provide some methods that are mainly used for operations regarding files.

For example, to create, write, and compare two path names, check whether a specific file is present, and many more. To understand this topic, first, consider one example for both approaches.

Note: To specify a directory is different in different operating systems (suppose a java file is in a folder named 'Folder' is created on the desktop)

In Linux and Mac:
/Users/Folder/

In Windows: ' \\ ' used instead of  ' / ' to escape ' \ ' character. So the same directory is accessed as
\\Users\\Folder\\

1. Using the File Class

It is a class that is just a handle for underlying file system resources. It is used for those objects which do not have physical existence.

Syntax:

File.createNewFile()

Example:

Java
// Create New file using File Class import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; public class CreateUsingFile {    // Function To Make New File  public void newFile()  {  String strPath , strName ;  // Try-catch Block  try {  // Creating BufferedReadered object  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  System.out.print("Enter the file name : ");  // Reading File name  strName = br.readLine();  System.out.print("Enter the file path : ");  // Reading File Path  strPath = br.readLine();  // Creating File Object  File file1 = new File(strPath + "\\" + strName + ".txt");  // Method createNewFile() method creates blank  // file.  file1.createNewFile();  }  // Try-Catch Block  catch (Exception ex1) {  System.out.print("Failed to create a file.");  }  }    public static void main(String args[]){    // Creating New File via function  CreateUsingFile gfg = new CreateUsingFile();  gfg.newFile();  } } 

Output:  

CreateFileUsingFile

Explanation: In the above example, the BufferedReader and InputStreamReader are used to take the file name and path as input from the user. A File object is created with the provided name and path. The createNewFile() method creates a blank file in the specified location. Exception Handled using a try-catch block to manage exceptions like invalid paths or permissions.

2. Using the FileOutputStream Class

It is an output stream that can be written to FileOutputStream JavaDoc. It is used for those objects which are already existing.

Syntax:

new FileOutputStream(String filePath);

Example: echo > myFile.txt

Below is the implementation of the FilesOutputStream Class:

Java
// Java Program to create new file // using FileOutputStream class import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.InputStreamReader; public class UsingFileOutputStream {  // Function To Create A New File  public void newFile()  {  String strFilePath , strFileName ;  // Try-Catch Block  try {  // Creating BufferClass Object  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    // Asking user for File Name  System.out.print("Enter the file name : ");  strFileName = br.readLine();    // Asking File path from User  System.out.print("Enter the file path : ");  strFilePath = br.readLine();  // Creating Object of FileOutputStream Class  FileOutputStream fos = new FileOutputStream(  strFilePath + "" + strFileName + ".txt");  } catch (Exception ex1) {  System.out.println("Exception Occurred");  }  }  public static void main(String args[])  {  // Creating File Object  UsingFileOutputStream gfg = new UsingFileOutputStream();  gfg.newFile();  } } 

Output: It will be the same as the previous one because just the approach to create a new file has changed the rest of the file name and the directory where it is added remains the same.

UsingFileOutputStream

Explanation: In the above example, the BufferedReader and InputStreamReader are used for user input. A FileOutputStream object is created for the specified file. The constructor initializes an empty file in the specified location. Exception managed using a try-catch block for scenarios such as invalid paths or permission issues.

Imoportant Point: This Program is efficient for Windows users but Linux and Mac Users can face problems as the file name can be like "\test.txt" so, resolve this issue use the statement mentioned below:

File file1 = new File(strPath + "" + strName + ".txt");

make sure the path given does end with / , i.e , "dir_path/"


Similar Reads

Practice Tags :