Menu
Topics Index
...
`
FilterInputStream is the wrapper class for the Java InputStream Class and it provides special features and improves the performance in reading. FilterInputStream overrides all the methods of InputStream.

The subclasses of FilterInputStream are Java BufferedInputStream, DataInputStream, PushbackInputStream, LineNumberInputStream.
Constructor :
Constructor Description
protected FilterInputStream(InputStream in) Creates a FilterInputStream object for the passed Inputstream Object
Important Methods :
Method Description
int available() Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
abstract int read() Reads the next byte of data from the input stream.
boolean markSupported() Tests if this input stream supports the mark and reset methods.
void mark(int readlimit) Marks the current position in this input stream.
void reset() This method repositions this stream to the position at the time the mark method was last called on this input stream.
void close() Closes this input stream and releases any system resources associated with the stream.
FilterInputStreamDemo
import java.io.*;

class FilteredInputStreamDemo
{
    public static void main(String[] args) throws IOException
    {
        File f = new File("E:/JavaPrograms/FolderOne/fileOne.txt");
        if(f.exists())
            System.out.println("File exists.");
        else
            System.out.println("No file found.");
         FilterInputStream fis = new BufferedInputStream(new FileInputStream(f)); // LINE A
        
         int i;
         System.out.println("Available bytes : "+ fis.available()); // LINE B
         System.out.println("Mark Supported : " + fis.markSupported()); // LINE C
         while ( (i = fis.read()) != -1) // LINE D
         {
             System.out.print((char)i);
         }
         fis.close();// LINE E
    }
}
OUTPUT

File exists.
Available bytes : 12
Mark Supported : true
MERIT CAMPUS

DESCRIPTION

At LINE A we created a FilterInputStream reference for BufferedInputStream Object.
At LINE B we are checking the no. of bytes in the file.
At LINE C we are checking whether the file is markSupported.
At LINE D we are reading data from file.
At LINE E we are closing the stream.

THINGS TO TRY
  • Create a file and Write I LOVE PROGRAMMING in to it.
  • Set mark at P in the file and set limit as 3 and reset the file.
  • Inovoke any method after closing the stream to see an java.io.IOException.
Rest of Methods :
Method Description
int read(byte[] b) Reads some number of bytes from the input stream and stores them into the buffer array b.
int read(byte[] b, int off, int len) Reads up to len bytes of data from the input stream into an array of bytes.
long skip(long n) Skips over and discards n bytes of data from this input stream.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App