Java Program to Create a New File
Last Updated :
28 Aug, 2025
There are two methods to create a New File in Java:
- 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
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 {
// 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();
}
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:
OutputExplanation:
- BufferedReader (with InputStreamReader) reads input from the keyboard.
- User enters the file name and path using readLine().
- A File object is created with the given path and name.
- createNewFile() is called to generate the file on disk.
- If any error occurs, it prints “Failed to create a file.”
2. Using the FileOutputStream Class
The FileOutputStream class in Java is used to create or write data to files in the form of bytes. It can create a new file (if it doesn’t exist) or overwrite the existing one, making it useful for handling binary as well as text data.
Syntax:
FileOutputStream fos = new FileOutputStream(String filePath);
Example: echo > myFile.txt
Below is the implementation of the FilesOutputStream Class:
Java
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 {
// 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.
OutputExplanation:
- A BufferedReader with InputStreamReader(System.in) reads input from the keyboard.
- The user enters a file name and file path using readLine().
- A FileOutputStream object is created with the full path (path + filename + ".txt").
- If the file does not exist, it is created. If it exists, it is overwritten.
- If an error occurs (e.g., invalid path), an exception message is displayed.
Note:
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 to 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/"
Related Article
Explore
Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java