首页 > 学院 > 开发设计 > 正文

FileChannel , 对于nio 还没有接触到, 以后有待深入.

2019-11-14 21:38:48
字体:
来源:转载
供稿:网友
FileChannel , 对于nio 还没有接触到, 以后有待深入.
 1 package test; 2  3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.nio.ByteBuffer; 6 import java.nio.channels.*; 7  8 public class Test17 9 {10     public static void main(String[] args) throws Exception11     {12         FileChannel fc_in = new FileInputStream("d:/t.txt").getChannel();13         FileChannel fc_out = new FileOutputStream("d:/out_j.txt").getChannel();14         for (ByteBuffer buf = ByteBuffer.allocate(1024 * 1024); (fc_in.read(buf)) != -1;)15         {                        //缓存大小, 设定我想一次读多大的数据.16             buf.flip(); //这个相当重要, 想象一下 老式打字机, 把打字真眼移到首页17             fc_out.write(buf);18             buf.clear(); //注意这个...写完必须清楚数据, 不然程序就卡在这了.19         }20         fc_in.close();21         fc_out.close();22     }23 }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表