Home Java Example Java Io Java FilterOutputStream Example

Related Tutorials


 
 

Share on Google+Share on Google+

Java FilterOutputStream Example

Advertisement
In this section we will discuss about the FilterOutputStream class in Java.

Java FilterOutputStream Example

In this section we will discuss about the FilterOutputStream class in Java.

java.io.FilterOutputStream class writes the filter data. write method in FilterOutputStream writes data to the output stream. All the output stream classes that filter output streams extends FilterOutputStream class. FilterOutputStream extends java.io.OutputStream and overrides its methods.

Constructor of FilterOutputStream

Constructor Name
Description
FilterOutputStream(OutputStream out) This constructor instantiate a FilterOutputStream which contains a reference of output stream.
Syntax : public FilterOutputStream(OutputStream out)

Methods in FilterOutputStream

Commonly used methods of FilterOutputStream

  • close() : This method is used to close the output streams and to release the resources associated with the stream.

    Syntax : public void close() throws IOException
     
  • flush() : This method is used to flush out the output stream and forces to write the buffered output bytes to the stream.

    Syntax : public void flush() throws IOException
     
  • write(byte[] b) : This method is used to write out the bytes of an array to the stream.

    Syntax : public void write(byte[] b) throws IOException
     
  • write(byte[] b, int off, int len) : This method is used to write out the specified length of bytes of specified array, started 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 bytes to the stream.

    Syntax : public void write(int b) throws IOException

Example

A simple example of FilterOutputStream is being given which will demonstrate you about how to write the data to the output stream using FilterOutputStream. In this example I have used the FilterOutputStream to write the bytes to the stream for this I have created an output stream to write the byte streams using FileOutputStream and then passed this output stream to the FilterOutputStream and wrote the bytes using write method of FilterOutputStream and then read that written data.

Source Code

FilterOutputStreamExample.java

import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.IOException;

public class FilterOutputStreamExample
{
   public static void main(String args[])
    {
       FileInputStream fis = null;
       FilterInputStream fltris = null;
       FileOutputStream fos = null;
       FilterOutputStream fltros = null;		
       try
          {
	fos = new FileOutputStream("xyz.txt");              
	fltros = new FilterOutputStream(fos);	
	System.out.println();
	System.out.println("Writing bytes to the file xyz.txt......");
	System.out.println();
	fltros.write(82);
	fltros.write(79);
	fltros.write(83);
	fltros.write(69);
	fltros.write(73);
	fltros.write(78);
	fltros.write(68);
	fltros.write(73);
	fltros.write(65);
	System.out.println();
	System.out.println("******* Contents has been written into the xyz.txt file***********");
	System.out.println();
	fis = new FileInputStream("xyz.txt");
               fltris = new BufferedInputStream(fis);
	System.out.println("Reading bytes from the file xyz.txt.......");
	System.out.println();
	int r;
	while((r= fltris.read()) != -1)
	 {
	    System.out.print((char)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(fltros != null)
                     {
                       try
                           {
		fltros.close();
                           }
                          catch(IOException ie)
                            {
		     System.out.println(ie);
                            }
                       }
	     if(fltris != null)
                     {
                       try
                           {
		fltris.close();
                           }
                          catch(IOException iex)
                            {
		     System.out.println(iex);
                            }
                       }
	      if(fos != null)
                     {
                       try
                           {
		fos.close();
                           }
                          catch(IOException iex1)
                            {
		     System.out.println(iex1);
                            }
                       }  
               }// end finally
      }// end main
}// end class

Output

When you will execute the above code then the output will be as follows :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 13, 2012

Related Tutorials

Discuss: Java FilterOutputStream Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com