BufferedReader mark Example
BufferedReader class mark example. This example shows you how to use mark method.
BufferedReader class mark example.public void mark(int) throws IOException Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
Here is the code
/*
* @ # Mark.java
* A class repersenting use to mark method
* of BufferedReader class in java.io package
* version 29 May 2008
* author Rose India
*/
import java.io.*;
public class Mark {
public static void main(String[] args) throws Exception {
// Create a new instance of a BufferedReader object using
// a StringReader.
String s = "This is the internal StringReader buffer.";
StringReader stringReader = new StringReader(s);
BufferedReader bufReader = new BufferedReader(stringReader);
// Read from the underlying StringReader.
char[] arr = new char[s.length()];
bufReader.read(arr, 0, arr.length - 14);
System.out.println(arr);
// Call mark after having read all but the last 10
// characters from the StringReader.
if (bufReader.markSupported()) {
bufReader.mark(s.length());
}
// Read the rest of the data from the underlying
// StringReader.
bufReader.read(arr, arr.length - 14, 14);
System.out.println(arr);
// Call reset and then re-read from the underlying
// StringReader.
bufReader.reset();
char[] arr2 = new char[s.length()];
bufReader.read(arr2);
System.out.println(arr2);
// Close the BufferedReader object and the underlying
// StringReader object.
stringReader.close();
bufReader.close();
}
} |
Output
This is the internal String
This is the internal StringReader buffer.
Reader buffer. |
|