Java PushbackInputStream unread() Example

PushbackInputStream class unread() method example. This example shows you how to use unread() method.


Syntax is : public void unread(int b) throws IOException
This method pushes back a byte by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value (byte)b.

Here is the code.

/**
 * @(#) UnreadPushbackInputStream.java
 * A class representing use of method unread() of PushbackInputStream 
class in java.io Package.
 * @Version  10-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class UnreadPushbackInputStream {

    public static void main(String[] argsthrows IOException {

        byte[] byteArray = new byte[]{'M''A''H''E''N''D''R''A'};
        File file = new File("Mahendra.txt");

        // Create object of FileOutputStream class for specified file.
        FileOutputStream fOutStream = new FileOutputStream(file);
        fOutStream.write(byteArray);

        // Create object of PushbackInputStream class for specified stream.
        InputStream inStream = new FileInputStream(file);
        PushbackInputStream pInStream = new PushbackInputStream(inStream);

        // read content of the file.
        int charValue = pInStream.read();
        System.out.println("First char of the stream : " (charcharValue);

        // unread method call.
        pInStream.unread(charValue);
        System.out.println("unread method call so next char will be first " +
                "char.");
        System.out.print("Next char of the stream : "+(charpInStream.read());
    }
}

output of the program.
 First char of the stream : M
unread method call so next char will be first char.
Next char of the stream : M

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

HOME | COPYRIGHT | CONTACT US