首页 > 编程语言 > 详细

将字节数组写出到文件的两种方式

时间:2016-10-31 07:24:45      阅读:249      评论:0      收藏:0      [点我收藏+]

方式1.ByteArrayInputStream

public static void writeBytesToFile() throws IOException{
        String s = "Some text";
        byte[] bs= s.getBytes();
        OutputStream out = new FileOutputStream("aaa.txt");
        InputStream is = new ByteArrayInputStream(bs);
        byte[] buff = new byte[1024];
        int len = 0;
        while((len=is.read(buff))!=-1){
            out.write(buff, 0, len);
        }
        is.close();
        out.close();
    }

 

方式2.FileChannel

public static void writeBytesToFile2() throws IOException{
        String s = "Some text2";
        byte[] bs= s.getBytes();
        
        ByteBuffer bb = ByteBuffer.wrap(bs);
        
        FileChannel fc = new FileOutputStream("aaaa.txt").getChannel();
        fc.write(bb);
        fc.close();
    }

 

将字节数组写出到文件的两种方式

原文:http://www.cnblogs.com/Iqiaoxun/p/6014630.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!