Thursday, 12 May 2011

Read byte array using ByteArrayInputStream Example

Here we read the byte array and put it on console.

Function to read the byte array:

public static void readByteArray(byte[] bytes){
ByteArrayInputStream bai = new ByteArrayInputStream(bytes);

int ch;

//read bytes from ByteArrayInputStream using read method
while((ch = bai.read()) != -1)
{
System.out.print((char)ch);
}


}



Using the above function:



String str = "ByteArrayInputStream Example!";

//get bytes from string using getBytes method
byte[] bytes = str.getBytes();
readByteArray(bytes);



For partial reading of byte array read here.

No comments:

Post a Comment