欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

InputStream流和byte[]数组互转 博客分类: java InputStream流和byte[]数组互转 

程序员文章站 2024-03-24 11:12:34
...

代码如下:

File fi = new File("C:/Users/Administrator/Desktop/知识点.png");
InputStream is = new FileInputStream(fi);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=is.read(b))>0){
bo.write(b,0,len);
}
is.close();
System.out.println(bo.toByteArray());

//byte[]转InputStream
InputStream ib = new ByteArrayInputStream(bo.toByteArray());
File fo1 = new File("C:/Users/Administrator/Desktop/ocxDemo/123.png");
OutputStream os1 = new FileOutputStream(fo1);
byte[] b1 = new byte[1024];
int len1 = 0;
while((len1=ib.read(b1))>0){
os1.write(b1);
}
ib.close();
os1.close();