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 - FilterReader Class
Selected Reading
Java - FilterReader ClassIntroductionThe Java.io.FilterReader class is for reading filtered character streams. Following are the important points about FilterReader −
Class declarationFollowing is the declaration for Java.io.FilterReader class − public abstract class FilterReader extends Reader FieldFollowing are the fields for Java.io.FilterReader class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Using close() with BufferedReader (Automatic Closure)The following example shows the usage of Java FilterReader close() method. FilterReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FilterReaderDemo {
public static void main(String[] args) {
try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
int data;
while ((data = fr.read()) != -1) { // Read character by character
System.out.print((char) data);
}
// No need to manually close, try-with-resources handles it
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "Hello")Let us compile and run the above program, this will produce the following result− Hello Explanation
Example - Using mark(int readLimit) with BufferedReaderThe following example shows the usage of Java FilterReader mark(int readAheadLimit) method. FilterReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FilterReaderDemo {
public static void main(String[] args) {
try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
System.out.println("Mark supported? " + fr.markSupported());
// Read and print first character
System.out.print((char) fr.read());
// Mark the current position
fr.mark(5); // Can read up to 5 characters before mark expires
// Read next two characters
System.out.print((char) fr.read());
System.out.print((char) fr.read());
// Reset back to the marked position
fr.reset();
// Read again from the marked position
System.out.print((char) fr.read());
System.out.print((char) fr.read());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if 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 Character at a Time Using BufferedReaderThe following example shows the usage of Java FilterReader read() method. FilterReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FilterReaderDemo {
public static void main(String[] args) {
try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
int data;
while ((data = fr.read()) != -1) { // Read character by character
System.out.print((char) data); // Convert integer to character
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if 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. |