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

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

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

标签:string   void   text   div   while   str   test   字节   etc   

方式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();
    }

 

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

标签:string   void   text   div   while   str   test   字节   etc   

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

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