用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();

}

posted on 2016-09-19 11:25  MaXianZhe  阅读(1313)  评论(0编辑  收藏  举报

导航