码迷,mamicode.com
首页 > 编程语言 > 详细

Java使用ByteBuffer读取大文件

时间:2016-10-24 14:05:17      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:input   stat   static   system   get   pen   tor   close   write   


import Java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ReadWriteCompare { public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStream("f:"+ File.separator +"IBM e-Mentor Program Kickoff Night 1105.pdf"); FileOutputStream fileOutputStream = new FileOutputStream("f:" + File.separator + "test.pdf"); FileChannel inChannel = fileInputStream.getChannel(); FileChannel outChannel= fileOutputStream.getChannel(); 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(); } }

 

Java使用ByteBuffer读取大文件

标签:input   stat   static   system   get   pen   tor   close   write   

原文地址:http://www.cnblogs.com/swbzmx/p/5992382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!