Open In App

Creating a file using FileOutputStream

FileOutputStream class belongs to byte stream and stores the data in the form of individual bytes. It can be used to create text files. A file represents storage of data on a second storage media like a hard disk or CD. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing objects) at a time. In such situations, the constructors in this class will fail if the file involved is already open.

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.



Important methods:

Following steps are to be followed to create a text file that stores some characters (or text):



  1. Reading data: First of all, data should be read from the keyboard. For this purpose, associate the keyboard to some input stream class. The code for using DataInputSream class for reading data from the keyboard is as:
    DataInputStream dis =new DataInputStream(System.in);

    Here System.in represent the keyboard which is linked with DataInputStream object

  2. Send data to OutputStream: Now , associate a file where the data is to be stored to some output stream. For this , take the help of FileOutputStream which can send data to the file. Attaching the file.txt to FileOutputStream can be done as:
    FileOutputStream fout=new FileOutputStream(“file.txt”);
  3. Reading data from DataInputStream: The next step is to read data from DataInputStream and write it into FileOutputStream . It means read data from dis object and write it into fout object, as shown here:
    ch=(char)dis.read();
    fout.write(ch);
  4. Close the file: Finally, any file should be closed after performing input or output operations on it, else the data of the may be corrupted. Closing the file is done by closing the associated streams. For example, fout.close(): will close the FileOutputStream ,hence there is no way to write data into the file.

Implementation:




//Java program to demonstrate creating a text file using FileOutputStream
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class Create_File
{
    public static void main(String[] args) throws IOException 
    {
        //attach keyboard to DataInputStream
        DataInputStream dis=new DataInputStream(System.in);
  
        // attach file to FileOutputStream
        FileOutputStream fout=new FileOutputStream("file.txt");
  
        //attach FileOutputStream to BufferedOutputStream
        BufferedOutputStream bout=new BufferedOutputStream(fout,1024);
        System.out.println("Enter text (@ at the end):");
        char ch;
  
        //read characters from dis into ch. Then write them into bout.
        //repeat this as long as the read character is not @
        while((ch=(char)dis.read())!='@')
        {
            bout.write(ch);
        }
        //close the file
        bout.close();
    }
}

If the Program is executed again, the old data of file.txt will be lost and any recent data is only stored in the file. If we don’t want to lose the previous data of the file, and just append the new data to the end of already existing data, and this can be done by writing true along with file name.

FileOutputStream fout=new FileOutputStream(“file.txt”,true);

Improving Efficiency using BufferedOutputStream

Normally, whenever we write data to a file using FileOutputStream as:

fout.write(ch);

Here, the FileOutputStream is invoked to write the characters into the file. Let us estimate the time it takes to read 100 characters from the keyboard and write all of them into a file.

Important methods of BufferedOutputStream Class:

Output:

C:\> javac Create_File.java
C:\> java Create_File
Enter text (@ at the end):
This is a program to create a file
@

C:/> type file.txt
This is a program to create a file

Related Articles:


Article Tags :