Java PushbackInputStream read() Example
PushbackInputStream class read() method example. This example shows you how to use read() method.
Syntax is : public int read() throws IOException
This method reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
Here is the code.
/**
* @(#) ReadPushbackInputStream.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 ReadPushbackInputStream {
public static void main(String[] args) throws 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 pushInStream = new PushbackInputStream(inStream);
// read content of the file.
System.out.print("Content of the file : ");
for (int c = pushInStream.read(); c != -1; c = pushInStream.read()) {
System.out.print((char) c);
}
}
} |
output of the program.
Content of the file : MAHENDRA
|
|