首页 > 编程语言 > 详细

Java IO: ByteArrayInputStream

时间:2015-08-17 06:28:59      阅读:148      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!