用bytebuffer进行文件的读写代码段

ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

   //Direct Buffer的效率会更高。
//   ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);     

   long start = System.currentTimeMillis();
while(true)
{
int eof = inChannel.read(byteBuffer);
if(eof == -1 ) break;
byteBuffer.flip();
    outChannel.write(byteBuffer);
byteBuffer.clear();
}
System.out.println("spending : " + (System.currentTimeMillis()-start));
inChannel.close();
outChannel.close();

}

原文地址:https://www.cnblogs.com/juniorMa/p/5884496.html