Java使用ByteBuffer读取大文件
2016-10-24 11:49
435 查看
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、mappedByteBuffer读取文件
- 使用NIO的FileChannel和ByteBuffer高效读取文件
- Java使用ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件的方法
- Java读取文件 利用MappedByteBuffer进行缓冲
- 使用MappedByteBuffer(映射文件)对excel表格进行随机定位和读取
- 使用MappedByteBuffer读取大文件(1G以上)和释放MappedByteBuffer的资源
- Java读取文件 利用MappedByteBuffer进行缓冲
- Java 读取文件 使用BufferReader出现中文乱码的解决办法
- java读取资源文件--使用文件流的方式读取
- 使用java读取配置文件(ini、properties)
- Java使用字符流读取文件
- 初学Java,IO之使用FileInputStream和FileReader读取文件(四十一)
- Java 使用DataInputStream将数据写入文件,使用FileReader读取演示
- java 读取properties文件,运用 使用properties
- 使用JAVA读取EXCEL文件里面的数据
- java 使用dom4j.jar读取xml文件
- java 使用相对路径读取文件
- 运用JAVA读取txt文件(BufferReader)
- 使用Java读取Excel文件内容
- java 使用相对路径读取文件