1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* Read byte array using ByteArrayInputStream Example This example shows how to use ByteArrayInputStream object created from array of bytes. Method 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! */ |
Add Comment