PushbackReader unread Example

PushbackReader class unread example. This example shows you how to use unread method.


PushbackReader class unread example.public void unread(int c) throws IOException Pushes back a single character by copying it to the front of the pushback buffer.

Here is the code

/**
 * @ # Unread2.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 Unread2 {

    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.";
        for (int i = 0; i < newS.length(); i++) {
            reader.unread(newS.charAt(i));
        }

        // Close the PushbackReader object and the underlying
        // StringReader object.
        stringReader.close();
        reader.close();
    }
}

Output
This is the internal buffer of StringReader.

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US