How to Create a new file in Java using 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:

FileOutputStream 

Example: echo > myFile.txt

Below is the implementation of the FilesOutputStream Class:

Java




// Java Program to create new file
// using FileOutputStream class
 
// Importing File Classes
import java.io.FileOutputStream;
 
// Importing BufferedReader Class for taking input
import java.io.BufferedReader;
 
// Importing as it converts bits to strings
import java.io.InputStreamReader;
 
// Function Helping Create New File
public class GFG {
 
    // Main Driver Method
    public static void main(String args[])
    {
        // Creating File Object
        GFG gfg = new GFG();
        gfg.newFile();
    }
 
    // 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));
            System.out.println("Enter the file name:");
 
            // Asking file name from User
            strFileName = br.readLine();
            System.out.println("Enter the file path:");
 
            // Asking file path from User
            strFilePath = br.readLine();
 
            // Creating Object of FileOutputStream Class
            FileOutputStream fos = new FileOutputStream(
                strFilePath + "" + strFileName + ".txt");
        }
 
        // Try-Catch Block
        catch (Exception ex1) {
        }
    }
}


 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.

Added file name            : newFile.txt
Added file name directory : /Users/mayanksolanki/Desktop/Folder/

Explanation of the above Program:

In the second example, the File class is not used to create a new File programmatically. 

  1. To create a new file using java language, “FileOutputStream” class is used here and “BufferedReader” &  “InputStreamReader” both are used to take file name and path from the user as input. These classes are located inside of “java.io” package. So to make use of those classes, it is necessary to import them at the beginning of the program.
  2. Class is created namely, “GFG”. And inside that class “main()” method is defined from which execution will be started.
  3. Inside of “main()” method object of the class is created. And this object is used to call the “newFile()” method.
  4. Outside of the main() method, the newFile() method is declared which is covers code for taking input from the user and create a file as per input.
  5. In this line, two blank strings are declared namely, strFileName, strFilePath. “strFileName and strFilePath are used to store the name and path of the file when the user gives this information.
  6. To take file name and path from the user as input, here BufferedReader class and InputStreamReader class are used. The object of BufferedReader “br” is useful to take input values given by the user.
  7. This line print some text to give an indication to the user like ”Enter the file name:”. To print text “println()” function is used.
  8. Here “readLine()” method is used to take input and store it, in strFileName and strFilePath.
  9. Here object of FileOutputStream class is created and as a parameter, file path and name are given to the constructor. In this line of code “.txt” is a format of the file. You can change it as per need. The object of FileOutputStream class is necessary to call methods provided in its class. For example, if the user wants to store some text in a newly created file programmatically then, the write() method is helpful.
  10. Lastly, enclosed by “try{ }” block. Because, readLine() method generates exception. So to handle that exception try, catch block is used.

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/mayanksolanki/Desktop/Folder/

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



Java Program to Create a New File

A File is an abstract path, it has no physical existence. It is only when “using” that File that the underlying physical storage is hit. When the file is getting created indirectly the abstract path is created. The file is one way, in which data will be stored as per requirement.

Steps to Create a New File in Java

1. Primary, in order to create a new file, inbuilt files, and functions are used which definitely will throw Exceptions here playing it safe. So in order to deal with it, we will be using Exception Handling Techniques. Here, we will use one of them known try-catch block techniques. 

2. Secondary, additional work is simply we will be importing File Class for which we will be importing File Class.

File object_name = new File(Directory)

Similar Reads

Methods to Create a New File in Java

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

1. Java Program to Create a new file 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....

2. How to Create a new file in Java using FileOutputStream class?

...

Contact Us