ByteArrayInputStream read Example
ByteArrayInputStream class read example. This example shows you how to use read method.
ByteArrayInputStream class read example.public int read() 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
/**
* @ # Read.java
* A class repersenting use to read method
* of ByteArrayInputStream class in java.io package
* version 03 June 2008
* author Rose India
*/
import java.io.*;
public class read {
public static void main(String args[]) throws Exception {
byte b[] = {1, 2, 3, 4, 5};
ByteArrayInputStream baStream = new ByteArrayInputStream(b);
if (baStream.markSupported()) {
baStream.mark(b.length);
}
baStream.reset();
System.out.println("Byte of data from input stream.");
//Returns the number of remaining bytes
while (baStream.available() != 0) {
//Read ByteArrayInputStream
System.out.println(baStream.read());
}
//Closing ByteArrayInputStream
baStream.close();
}
} |
Output
Byte of data from input stream.
1
2
3
4
5 |
|