Java IO相互转换

byte[]和InputStream的相互转换

byte[]转换为InputStream

1
2
3
public static final InputStream bytes2InStream(byte[] buf) {
return new ByteArrayInputStream(buf);
}

InputStream转换为byte[]

1
2
3
4
5
6
7
8
9
10
public static final byte[] inStream2bytes(InputStream inStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = 0;
while ((len = inStream.read(buff)) > 0) {
baos.write(buff, 0, len);
}
byte[] bytes = baos.toByteArray();
return bytes;
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :