zoukankan      html  css  js  c++  java
  • Java使用ByteBuffer读取大文件


    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(); } }
  • 相关阅读:
    pinus学习(3)
    pinus学习(2)
    HBASE架构解析(二)
    排序算法
    泛型原理
    《JAVA NIO》第二章缓冲区
    @SuppressWarnings
    网络编程
    synchronized原理
    第十六节:pandas之日期时间
  • 原文地址:https://www.cnblogs.com/swbzmx/p/5992382.html
Copyright © 2011-2022 走看看