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 - Reader Class
Selected Reading
Java - Reader ClassIntroductionThe Java Reader class is a abstract class for reading character streams. Class declarationFollowing is the declaration for Java.io.Reader class − public class Reader
extends Object
implements DataOutput, DataInput, Closeable
FieldFollowing are the fields for Java.io.Reader class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Using close() Manually with StringReaderThe following example shows the usage of Reader close() method. ReaderDemo.javapackage com.tutorialspoint;
import java.io.IOException;
import java.io.StringReader;
public class ReaderDemo {
public static void main(String[] args) {
String data = "This is a sample string.";
StringReader reader = null;
try {
reader = new StringReader(data);
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
System.out.println("\nReader closed successfully.");
}
}
}
}
OutputLet us compile and run the above program, this will produce the following result− This is a sample string. Reader closed successfully. Explanation
Example - Mark and Reset within the LimitThe following example shows the usage of Reader mark(int readAheadLimit) method. ReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class ReaderDemo {
public static void main(String[] args) {
String data = "ABCDEFG";
try (BufferedReader reader = new BufferedReader(new StringReader(data))) {
System.out.print((char) reader.read()); // A
reader.mark(3); // Mark after reading 'A'
System.out.print((char) reader.read()); // B
System.out.print((char) reader.read()); // C
reader.reset(); // Go back to after 'A'
System.out.print((char) reader.read()); // B (again)
System.out.print((char) reader.read()); // C (again)
System.out.print((char) reader.read()); // D
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− ABCBCD Explanation
Example - Using BufferedReader (Supports Mark)The following example shows the usage of Reader markSupported() method. ReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.StringReader;
public class ReaderDemo {
public static void main(String[] args) {
String data = "Hello World";
BufferedReader reader = new BufferedReader(new StringReader(data));
System.out.println("markSupported(): " + reader.markSupported());
try {
reader.mark(5); // This is safe
System.out.println("Marked at start");
System.out.print((char) reader.read()); // H
System.out.print((char) reader.read()); // e
reader.reset(); // Resets to 'H'
System.out.print("\nAfter reset: ");
System.out.print((char) reader.read()); // H again
} catch (Exception e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− markSupported(): true Marked at start He After reset: H 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. |