码迷,mamicode.com
首页 > 编程语言 > 详细

Java IO: ByteArrayInputStream

时间:2015-08-17 06:31:18      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

 

 

The ByteArrayInputStream class of the Java IO API allows you to read data from byte arrays as streams. Here is a simple example:

byte[] bytes = ... //get byte array from somewhere.

InputStream input = new ByteArrayInputStream(bytes);

int data = input.read();
while(data != -1) {
  //do something with data

  data = input.read();
}
input.close();    

  

ByteArrayInputStream‘s can be handy if your data is stored in an array, but you have a component that can only process it as an InputStream. The ByteArrayInputStream can thus wrap the byte array, and turn it into a stream.

Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.

 

Java IO: ByteArrayInputStream

标签:

原文地址:http://www.cnblogs.com/hephec/p/4735462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!