Java BufferedInputStream read() Example
BufferedInputStream 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 bytes from this byte-input stream into the specified byte array, starting at the given offset. If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1. Otherwise this method returns the number of bytes actually read.
Here is the code.
/**
* @(#) Read1BufferedInputStream.java
* A class representing use of method read() of BufferedInputStream class in
java.io Package.
* @Version 03-May-2008
* @author Rose India Team
*/
import java.io.*;
public class Read1BufferedInputStream {
public static void main(String[] args) throws IOException{
// Declare the buffer and initialize its size:
byte[] arrByte = new byte[1024];
/* Construct an buffer input stream using some String value.
This is deprecated method in java api 1.6. */
StringBufferInputStream obj = new StringBufferInputStream("MAHENDRA");
// Construct the buffer using the buffer input stream obj.
BufferedInputStream bin = new BufferedInputStream(obj);
int c = 0;
System.out.print("Elements in buffer are : ");
// Read from the buffer one character at a time:
while ((c = bin.read(arrByte, 0, arrByte.length)) >= 0){
for (int i = 0; i < c; i++) {
// Display the read byte:
System.out.print((char)arrByte[i]);
}
}
}
} |
Output of the program.
| Elements in buffer are : MAHENDRA |
|