In this tutorial we will discuss about the FilterInputStream class in Java.
Java FilterInputStream Example
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 :
- available() : This method is used to find out the number
of bytes that can be read from the input stream.
Syntax : public int available() throws IOException
- close() : This method is used to close the stream and it
release the resources associated to the stream.
Syntax : public void close() throws IOException
- mark(int readlimit) : This method is used to mark the
current position in the input stream.
Syntax : public void mark(int readlimit)
- markSupported() : This method is used to check out whether
the input stream supported the mark and reset method.
Syntax : public boolean markSupported()
- read() : This method is used to read the bytes from the
input stream.
Syntax : public int read() throws IOException
- read(byte[] b) : From the input stream this method reads
bytes of data from an array of bytes.
Syntax : public int read(byte[] b) throws IOException
- read(byte[] b, int off, int len) : This method is used to
read the specified length of data 'len' from the specified array of bytes, and
started from the specified offset 'off'.
Syntax : public int read(byte[] b, int off, int len) throws IOException
- reset() : This method is used to reset i.e. to repositioned
the input stream where the mark method was marked the position.
Syntax : public void reset() throws IOException
- skip(long n) : This method is used to skip the specified
number of bytes from the input stream.
Syntax : public long skip(long n) throws IOException
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 :