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
| 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
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 :

|
Recommend the tutorial |
Ask Questions? Discuss: Java FilterOutputStream Example
Post your Comment