StringReader class reset()method example
Resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.
StringReader class reset()method example. This example shows you how to use reset()method.This method Resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.
Here is the code:-
/**
* @Program that Resets the stream to the most recent mark, or to the
* beginning of the string
* Reset.java
* Author:-RoseIndia Team
* Date:-29-May-2008
*/
import java.io.*;
public class Reset {
public static void main(String[] args) {
try {
// Create a new StringReader.
String s = "RoseIndia";
StringReader reader = new StringReader(s);
// Read from the underlying string one at a time.
char l = (char) reader.read();
// Set the mark.
if (reader.markSupported()) {
// Marks the present position in the stream
reader.mark(0);
}
// Continue reading from the string.
char l1 = (char) reader.read();
char l2 = (char) reader.read();
char l3 = (char) reader.read();
char l4 = (char) reader.read();
// Reset the StringReader.
reader.reset();
// Get the first character read after the reset.
char nextLetter = (char) reader.read();
System.out.println("The next letter Readed after reset is: " +
nextLetter);
// Close the StringReader.
reader.close();
} catch (IOException ex) {
System.out.println(ex.toString());
}
}
}
|
Output of the program:-
| The next letter Readed after reset is: o |
|