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

java将InputStream转换成字节数组

时间:2015-11-27 12:51:13      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

public class InputStream2ByteArray {

    public static void main(String[] args) throws IOException {
        InputStream in=new FileInputStream("/tmp/req.data");
        byte[] data=toByteArray(in);
        in.close();
        FileOutputStream out=new FileOutputStream("/tmp/req2.data");
        out.write(data);
        out.close();
    }

    public static byte[] toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024*4];
        int n=0;
        while ( (n=in.read(buffer)) !=-1) {
            out.write(buffer,0,n);
        }
        return out.toByteArray();
    }
}

 

java将InputStream转换成字节数组

标签:

原文地址:http://www.cnblogs.com/sprinng/p/5000044.html

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