Java FileOutputStream Example

In this section we will discuss about the Java IO FileOutputStream.

Java FileOutputStream Example

In this section we will discuss about the Java IO FileOutputStream.

Java FileOutputStream Example

Java FileOutputStream Example

In this section we will discuss about the Java IO FileOutputStream.

FileOutputStream is a class of java.io package which facilitate to write output stream data to a file. The FileOutputStream writes a streams of raw bytes. OutputStream is a base class of FileOutputStream class.

To write output streams there are several constructors of FileOutputStream is provided to the developer which facilitate to write streams in various ways Some are as follows :

Constructor Name
Description
FileOutputStream(File file) This constructor creates a file output stream to write the output stream data to a specified file (File object).
FileOutputStream(File file, boolean append) This constructor creates a file output stream to write the output stream data to a specified file (File object). This constructor allows to write data output stream by appending the existing text (if any) into the specified file if the value of boolean is 'true' otherwise the new text will overwrite the existing text into the file.
FileOutputStream(String name) This constructor creates output file stream to write the output stream data to a specified file given as String.
FileOutputStream(String name, boolean append) This constructor creates output file stream to write the outputs stream data to a specified file given as String. If the value of boolean is 'true' then the data output stream will be written to the file by appending the existing text otherwise, the new text will overwrite the existing text into the file.

Commonly used methods of FileOutputStream class

Method Name
Description
close() This method is used to close the output stream and release the system resource if any, associated with the stream.
Syntax : public void close() throws IOException
finalize() When this method is called it clears the connections from the file and confirm that the close() method of this class is called.
Syntax : protected void finalize() throws IOException
write(byte[] b) This method is used to write the bytes of the specified length (e.g. b.length) to the specified file.
Syntax : public void write(byte[] b) throws IOException
write(byte[] b, int off, int len) This method is used to write the bytes of the specified length len from the specified array, and the byte is started to write from the specified offset off.
Syntax : public void write(byte[] b, int off, int len) throws IOException
write(int b) This method is used to write the specified byte.
Syntax : public void write(int b) throws IOException

Example :

Here an example is being given which demonstrates how to write the data output stream in to the file using FileOutputStream. For this I have created two text files named "abc.txt" and "xyz.txt". abc.txt is a text file that contains some data which will be further read by the program and the xyz.txt is a blank text file into which the text read from the abc.txt file will be written. Now created a Java class named WriteFileOutrputStreamExample.java where I have used the FileInputStream to read the input stream from the file 'abc.txt' and used FileOutputStream to write the output stream data into the 'xyz.txt'. To write the specified bytes into the xyz.txt file I have used the write(int ....) method of the FileOutputStream class. And finally close all the input stream and output stream that the resources associated with this stream can be released.

Source Code

/*This example demonstrates that how
to write the data output stream to the 
FileOutputStream*/

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteFileOutputStreamExample
{
   public static void main(String args[])
    {
       FileInputStream fis = null;
       FileOutputStream fos = null;		
       try
          {
	fis = new FileInputStream("abc.txt");
	fos = new FileOutputStream("xyz.txt");
	int r;
	System.out.println();
	System.out.println("******* Contents of abc.txt file has been written into the xyz.txt 

***********");
	System.out.println();
	while((r= fis.read()) != -1)
	 {
	    fos.write(r);
	  }
	System.out.println();
           }
           catch(IOException e)
            {
	System.out.println("IOException caught..!!");
	e.printStackTrace();
             }
	finally
	 {
	    if(fis != null)
	     {
	       try
	         {
	           fis.close();
	         }
	       catch (IOException ioe)
	        {
		System.out.println(ioe);
	        }
	      if(fos != null)
                     {
                       try
                           {
		fos.close();
                           }
                          catch(IOException ie)
                            {
		     System.out.println(ie);
                            }
                       }
	     }
               }// end finally
      }// end main
}// end class

Output :

When you will execute the above example a message will print on the console and the content of abc.txt file will be written to the xyz.txt file.

Following steps are required to execute the above example :

1. First compile the WriteFileOutputStreamExample.java using javac command provided in the JDK

as javac WriteFileOutputStreamExample.java

2. After successfully compilation execute the class by using java command provided in the JDK.

as java WriteFileOutputStreamExample

Download Source Code