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 Exception
11     {
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 }

 

posted @ 2014-10-25 18:48  testman00  阅读(218)  评论(0编辑  收藏  举报