基础的IO类代码,有2个方法的意思不太理解,大家帮忙解释下,谢谢(不用看代码)
import java.nio.*;
import java.nio.channels.*;
import java.io.*;public class ChannelCopy {
  private static final int BSIZE = 1024;
  public static void main(String[] args) throws Exception {
    if(args.length != 2) {
      System.out.println("arguments: sourcefile destfile");
      System.exit(1);
    }
    FileChannel
      in = new FileInputStream(args[0]).getChannel(),
      out = new FileOutputStream(args[1]).getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
    while(in.read(buffer) != -1) {
      buffer.flip(); //JDK上说是反转,但是似乎不是我所理解的反转意思,原来是 abc flip()后变成 cba(这是我对反转的理解)
      out.write(buffer);
      buffer.clear();  // 这里调用clear()有什么意义?
    }
  }
}2、还想问大家个问题。虽然我对IO类的代码基本能看懂(暂时看到的那些),但是特别容易忘,而且还搞不大清楚(又是InputStream、OutputStream、Read、Write、FileChannel、ByteBuffer乱七八糟一堆,而且继承层次也很到,很容易晕)。总觉得学了很容易忘,过两天变的跟没学一样,大家有什么好的建议吗?

解决方案 »

  1.   

    buffer.flip(); //JDK上说是反转,但是似乎不是我所理解的反转意思,原来是 abc flip()后变成 cba(这是我对反转的理解)
    ================
    你的理解是对字符串反转的理解
    详细:
    http://bbs.tarena.com.cn/viewthread.php?tid=125914
      

  2.   

    2、还想问大家个问题。虽然我对IO类的代码基本能看懂(暂时看到的那些),但是特别容易忘,而且还搞不大清楚(又是InputStream、OutputStream、Read、Write、FileChannel、ByteBuffer乱七八糟一堆,而且继承层次也很到,很容易晕)。总觉得学了很容易忘,过两天变的跟没学一样,大家有什么好的建议吗?
    ===========================================
    我也是,多用多看就好了
      

  3.   

    1.buffer.clear();  // 这里调用clear()有什么意义?
      将buffer清空,可以使每次的while循环都可以直接写入文件中,
      如果不调用的话,就有机会不是每次循环都可以及时写入文件。2.顾名思义,InputStream就是输入流,通过它就可以读取数据,它是抽象类,其它的XXXInputStream都是继承它。
      OutputStream就是输出流了,可以通过它来写入一些数据,也是抽象类,其他的输出流也是继承它。
      Read就是阅读器,可以读取输入流的数据,反正你在用的时候,尽量去思考下,为什么这样写,就很容易熟悉和掌握啦
      

  4.   

    flip
    public final Buffer flip()Flips this buffer. The limit is set to the current position and then the position is set to zero. If the  is defined then it is discarded.
    这个是javadoc的说明,flip应该只是讲buffer中的limit赋值为position,讲position赋值为0,而和buffer中的内容无关。
    clear
    public final Buffer clear()
    Clears this buffer. The position is set to zero, the limit is set to the capacity, and the  is discarded.

    This method does not actually erase the data in the buffer这个方法也是只是设置重新设置一下limit和position的值,并且最后一句话说明,这个方法并不清空buffer。