In this tutorial we will discuss about the FilterInputStream class in Java.
java.io.FilterInputStream class reads the filtered data. read method in FilterInputStream reads input from the stream. This class extends the java.io.InputStream class and overrides its methods. All the input stream classes that passes the filtered data are the subclasses of FilterInputStream class.
Constructor of FilterInputStream
| FilterInputStream(InputStream in) | This constructor instantiate the FilterInputStream with the reference of an
InputStream. Syntax : protected FilterInputStream(InputStream in) |
Methods in FilterInputStream
Commonly used methods of FilterInputStream class are as follows :
Example
Here I am giving a simple example of FilterInputStream which reads the bytes from the input stream. In this example I have created a class named FilterInputStreamExample.java into which created an input stream using FileInputStream which reads data from a text file created in the same directory. Then instantiated FilterInputStream using the BufferedInputStream and passed the reference of input stream then read the bytes from the input stream using read method in FilterInputStream.
Source Code
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class FilterInputStreamExample {
public static void main(String[] args) throws Exception {
FileInputStream is = null;
FilterInputStream fis = null;
try
{
is = new FileInputStream("data.txt");
fis = new BufferedInputStream(is);
int r;
System.out.println();
while ((r = fis.read()) != -1)
{
System.out.print((char)r);
}
System.out.println();
}
catch(IOException e)
{
System.out.println(e);
}
finally
{
if(is!=null)
is.close();
if(fis!=null)
fis.close();
}
}
}
Output :
When you will execute the above code you will get the output as follows :

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