
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - FilterInputStream Class
Introduction
The Java FilterInputStream class contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality. Following are the important points about FilterInputStream −
The class itself simply overrides all methods of InputStream with versions that pass all requests to the contained input stream.
The Subclasses of this class may further override some of these methods and may also provide additional methods and fields.
Class declaration
Following is the declaration for Java.io.FilterInputStream class −
public class FilterInputStream extends InputStream
Field
Following are the fields for Java.io.FilterInputStream class −
protected InputStream in − This is the input stream to be filtered.
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 |
protected FilterInputStream(InputStream in) This creates a FilterInputStream by assigning the argument in to the field this.in to remember it for later use. |
Class methods
Sr.No. | Method & Description |
---|---|
1 |
int available()
This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. |
2 |
void close()
This method closes this input stream and releases any system resources associated with the stream. |
3 |
void mark(int readlimit)
This method marks the current position in this input stream. |
4 |
boolean markSupported()
This method tests if this input stream supports the mark and reset methods. |
5 |
int read()
This method reads the next byte of data from this input stream. |
6 |
int read(byte[] b)
This method reads up to byte.length bytes of data from this input stream into an array of bytes. |
7 |
int read(byte[] b, int off, int len)
This method reads up to len bytes of data from this input stream into an array of bytes. |
8 |
void reset()
This method repositions this stream to the position at the time the mark method was last called on this input stream. |
9 |
long skip(long n)
This method skips over and discards n bytes of data from this input stream. |
Methods inherited
This class inherits methods from the following classes −
- Java.io.Object
Example - Closing a BufferedInputStream (Subclass of FilterInputStream)
The following example shows automatically closing of BufferedInputStream.
FilterInputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FilterInputStream; import java.io.IOException; public class FilterInputStreamDemo { public static void main(String[] args) { try (FilterInputStream fis = new BufferedInputStream(new FileInputStream("example.txt"))) { int data; while ((data = fis.read()) != -1) { System.out.print((char) data); } // The stream is automatically closed here due to try-with-resources } catch (IOException e) { e.printStackTrace(); } } }
Output(assuming example.txt contains JavaProgramming)
Let us compile and run the above program, this will produce the following result−
JavaProgramming
Explanation
Creates a BufferedInputStream, which extends FilterInputStream, to read "example.txt".
Reads file contents byte by byte and prints them.
Uses try-with-resources, so close() is automatically called at the end.
Example - Using mark(int readlimit) with BufferedInputStream
The following example shows the usage of Java FilterInputStream mark(int readLimit) method.
FilterInputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FilterInputStream; import java.io.IOException; public class FilterInputStreamDemo { public static void main(String[] args) { try (FilterInputStream fis = new BufferedInputStream(new FileInputStream("example.txt"))) { System.out.println("Mark supported? " + fis.markSupported()); // Read and print first character System.out.print((char) fis.read()); // Mark the current position fis.mark(5); // Can read up to 5 bytes before the mark is invalid // Read next two characters System.out.print((char) fis.read()); System.out.print((char) fis.read()); // Reset back to marked position fis.reset(); // Read again from the marked position System.out.print((char) fis.read()); System.out.print((char) fis.read()); } catch (IOException e) { e.printStackTrace(); } } }
Output(assuming example.txt contains Hello)
Let us compile and run the above program, this will produce the following result−
Mark supported? true Helo elo
Explanation
Checks if marking is supported (markSupported()).
Marks the position after reading one character (mark(5)) with a read limit of 5.
Reads two more characters.
Calls reset(), which moves the stream back to the marked position.
Reads the same characters again.
Example - Reading One Byte at a Time Using BufferedInputStream
The following example shows the usage of Java FilterInputStream read() method.
FilterInputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FilterInputStream; import java.io.IOException; public class FilterInputStreamDemo { public static void main(String[] args) { try (FilterInputStream fis = new BufferedInputStream(new FileInputStream("example.txt"))) { int data; while ((data = fis.read()) != -1) { // Read byte by byte System.out.print((char) data); // Convert byte to char and print } } catch (IOException e) { e.printStackTrace(); } } }
Output(assuming example.txt contains Hello)
Let us compile and run the above program, this will produce the following result−
Hello
Explanation
Uses BufferedInputStream (a subclass of FilterInputStream).
Reads one byte at a time using read().
Converts byte to character ((char) data) and prints it.
Stops when read() returns -1 (EOF).