Java PushbackInputStream read() Example

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


Syntax is : public int read(byte[] b, int off, int len) throws IOException
This method reads up to len bytes of data from this input stream into an array of bytes. This method first reads any pushed-back bytes; after that, if fewer than len bytes have been read then it reads from the underlying input stream. If len is not zero, the method blocks until at least 1 byte of input is available; otherwise, no bytes are read and 0 is returned.

Here is the code.

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

public class Read1PushbackInputStream {

    public static void main(String[] argsthrows IOException {

        // Declare the buffer and initialize its size:
        byte[] arrByte = new byte[1024];

        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 pushInStream = new PushbackInputStream(inStream);

        System.out.print("Content of the file : ");
        // Read from the buffer one character at a time:
        int c = 0;
        while ((c = pushInStream.read(arrByte, 0, arrByte.length)) >= 0) {
            for (int i = 0; i < c; i++) {

                // Display the read byte:
                System.out.print((chararrByte[i]);
            }
        }
    }
}

output of the program.
 Content of the file : MAHENDRA

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

HOME | COPYRIGHT | CONTACT US