GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - FilterInputStream Class
Selected Reading
Java - FilterInputStream ClassIntroductionThe 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 −
Class declarationFollowing is the declaration for Java.io.FilterInputStream class − public class FilterInputStream extends InputStream FieldFollowing are the fields for Java.io.FilterInputStream class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Closing a BufferedInputStream (Subclass of FilterInputStream)The following example shows automatically closing of BufferedInputStream. FilterInputStreamDemo.javapackage 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
Example - Using mark(int readlimit) with BufferedInputStreamThe following example shows the usage of Java FilterInputStream mark(int readLimit) method. FilterInputStreamDemo.javapackage 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
Example - Reading One Byte at a Time Using BufferedInputStreamThe following example shows the usage of Java FilterInputStream read() method. FilterInputStreamDemo.javapackage 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
Advertisements
| ||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |