PushbackReader unread Example
PushbackReader class unread example. This example shows you how to use unread method.
PushbackReader class unread example.public void unread(char[] cbuf) throws IOException Pushes back an array of characters by copying it to the front of the pushback buffer. After this method returns, the next character to be read will have the value cbuf[0], the character after that will have the value cbuf[1], and so forth.
Here is the code
/**
* @ # Unread.java
* A class repersenting use to unread method
* of PipedWriter class in java.io package
* version 31 May 2008
* author Rose India
*/
import java.io.*;
public class Unread {
public static void main(String args[]) throws Exception {
// Create a new instance of a PushbackReader object with an
// underlying StringReader buffer.
String s = "This is the internal buffer of StringReader.";
StringReader stringReader = new StringReader(s);
PushbackReader reader = new PushbackReader(stringReader, 100);
// Read from the underlying StringReader buffer.
char[] arr = new char[s.length()];
if (reader.ready()) {
reader.read(arr, 0, arr.length);
}
System.out.println(arr);
// "Unread" some of the data read from the underlying
// StringReader buffer.
String newS = "Replacement internal buffer.";
char[] newSArr = newS.toCharArray();
reader.unread(newSArr);
// Close the PushbackReader object and the underlying
// StringReader object.
stringReader.close();
reader.close();
}
} |
Output
| This is the internal buffer of StringReader. |
|