/*
Read byte array using ByteArrayInputStream Example
This example shows how to use ByteArrayInputStream object created
from array of bytes.
Mehod of this class can be called even after calling the close method
without causing the IOException. Closing ByteArrayInputStream has
no effect.
*/
import java.io.ByteArrayInputStream;
public class ByteArrayInputStreamExample {
public static void main(String[] args) {
String str = "ByteArrayInputStream Example!";
//get bytes from string using getBytes method
byte[] bytes = str.getBytes();
//create ByteArrayInputStream object
ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
int ch;
//read bytes from ByteArrayInputStream using read method
while((ch = bai.read()) != -1)
{
System.out.print((char)ch);
}
}
}
/*
Output of this program would be
ByteArrayInputStream Example!
*/
Bookmark/Search this post with:
Post new comment