ByteArrayOutputStream toByteArray Example
ByteArrayOutputStream class toByteArray example. This example shows you how to use toByteArray method.
ByteArrayOutputStream class toByteArray example.public byte[] toByteArray() Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
Here is the code
/**
* @ # ToByteArray.java
* A class reprsenting use to toByteArray method
* of ByteArrayOutputStream class in java.io package
* version 09 June 2008
* author Rose India
*/
import java.io.*;
public class ToByteArray {
public static void main(String args[])throws Exception{
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
String s = "Rose India";
byte bs[] = s.getBytes();
//Writes a byte to the byte array output stream.
oStream.write(bs);
byte b[] = oStream.toByteArray();
for (int i = 0; i < b.length; i++) {
System.out.print((char)b[i]);
}
}
} |
|