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 42 43 44 |
/* Read partial byte array using ByteArrayInputStream Example This example shows how to create ByteArrayInputStream from partial array of bytes using offset and length arguments of ByteArrayInputStream constructor. 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 ReadPartialByteArray { public static void main(String[] args) { String str = "Byte Array InputStream Example"; //get bytes from string byte[] bytes = str.getBytes(); /*use below given constructor of ByteArrayInputStream to * create stream of partial byte array, * * ByteArrayInputStream(byte[] b, int offset, int length) */ ByteArrayInputStream bis = new ByteArrayInputStream(bytes, 5, 5); int ch; while ((ch = bis.read()) != -1) { System.out.print((char)ch); } } } /* Output of this program would be Array */ |
Add Comment