读取文件大小:1.45G 
第一种,OldIO:

  1. public static void oldIOReadFile() throws IOException{
  2. BufferedReader br = new BufferedReader(new FileReader("G://lily_947.txt"));
  3. PrintWriter pw = new PrintWriter("G://oldIO.tmp");
  4. char[] c = new char[100*1024*1024];
  5. for(;;){
  6. if(br.read(c)!=-1){
  7. pw.print(c);
  8. }else{
  9. break;
  10. }
  11. }
  12. pw.close();
  13. br.close();
  14. }

耗时70.79s

第二种,newIO:

  1. public static void newIOReadFile() throws IOException{
  2. FileChannel read = new RandomAccessFile("G://lily_947.txt","r").getChannel();
  3. FileChannel writer = new RandomAccessFile("G://newIO.tmp","rw").getChannel();
  4. ByteBuffer bb = ByteBuffer.allocate(200*1024*1024);
  5. while(read.read(bb)!=-1){
  6. bb.flip();
  7. writer.write(bb);
  8. bb.clear();
  9. }
  10. read.close();
  11. writer.close();
  12. }

耗时47.24s

第三种,RandomAccessFile:

  1. public static void randomReadFile() throws IOException{
  2. RandomAccessFile read = new RandomAccessFile("G://lily_947.txt","r");
  3. RandomAccessFile writer = new RandomAccessFile("G://random.tmp","rw");
  4. byte[] b = new byte[200*1024*1024];
  5. while(read.read(b)!=-1){
  6. writer.write(b);
  7. }
  8. writer.close();
  9. read.close();
  10. }

耗时46.65

第四种,MappedByteBuffer:

  1. public static void mappedBuffer() throws IOException{
  2. FileChannel read = new FileInputStream("G://lily_947.txt").getChannel();
  3. FileChannel writer = new RandomAccessFile("G://buffer.tmp","rw").getChannel();
  4. long i = 0;
  5. long size = read.size()/30;
  6. ByteBuffer bb,cc = null;
  7. while(i<read.size()&&(read.size()-i)>size){
  8. bb = read.map(FileChannel.MapMode.READ_ONLY, i, size);
  9. cc = writer.map(FileChannel.MapMode.READ_WRITE, i, size);
  10. cc.put(bb);
  11. i+=size;
  12. bb.clear();
  13. cc.clear();
  14. }
  15. bb = read.map(FileChannel.MapMode.READ_ONLY, i, read.size()-i);
  16. cc.put(bb);
  17. bb.clear();
  18. cc.clear();
  19. read.close();
  20. writer.close();
  21. }

耗时:36

前三种读法对应的资源占用图如下: 
相对于最后一种内存直接映射方式前面的测试其实无意义,基本秒杀。。。。。 
对于很大的文件直接分块映射时内存会不够,这是因为MappedByteBuffer未被释放造成的,sun未提供直接回收MappedByteBuffer区域的方法,这个时候有两种方法解决,第一种比较愚笨的:

  1. System.gc();
  2. System.runFinalization();
  3. try {
  4. Thread.sleep(3000);
  5. } catch (InterruptedException e) {
  6. e.printStackTrace();
  7. }

第二种网上找来的,利用反射调用clean方法:

  1. public static void unmap(final MappedByteBuffer buffer) {
  2. if (buffer == null) {
  3. return;
  4. }
  5. AccessController.doPrivileged(new PrivilegedAction<Object>() {
  6. public Object run() {
  7. try {
  8. Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]);
  9. if (getCleanerMethod != null) {
  10. getCleanerMethod.setAccessible(true);
  11. Object cleaner = getCleanerMethod.invoke(buffer, new Object[0]);
  12. Method cleanMethod = cleaner.getClass().getMethod("clean", new Class[0]);
  13. if (cleanMethod != null) {
  14. cleanMethod.invoke(cleaner, new Object[0]);
  15. }
  16. }
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. return null;
  21. }
  22. });
  23. }

以上两种方法感觉都别扭,还有就是可以自己分割成物理文件再循环调用,这个也不太美观。 
速度也会减慢好多。

当逐行读写大于2G的文本文件时推荐使用以下代码

void largeFileIO(String inputFile, String outputFile) {

try {

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(inputFile)));

BufferedReader in = new BufferedReader(new InputStreamReader(bis, "utf-8"), 10 * 1024 * 1024);//10M缓存

FileWriter fw = new FileWriter(outputFile);

while (in.ready()) {

String line = in.readLine();

fw.append(line + "");

}

in.close();

fw.flush();

fw.close();

} catch (IOException ex) {

ex.printStackTrace();

}

Java IO读写大文件的几种方式及测试的更多相关文章

  1. java 从网上下载文件的几种方式

    package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import ...

  2. C/C++读写excel文件 的几种方式

    因为有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看. http://blog.csdn.net/fullsail/article/details/8449448 C++读取Exc ...

  3. IO之复制文件的四种方式

    1. 使用FileStreams复制 这是最经典的方式将一个文件的内容复制到另一个文件中. 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B. 这是 ...

  4. Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式

    Linux就这个范儿 第15章 七种武器  linux 同步IO: sync.fsync与fdatasync   Linux中的内存大页面huge page/large page  David Cut ...

  5. java指定编码的按行读写txt文件(几种读写方式的比较)

    转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976  版权声明:本文为博主原创文章,未经博主允许不得转载. ...

  6. java io读写文件

    java io读写文件相关阅读:http://www.cnblogs.com/wing011203/archive/2013/05/03/3056535.html public class DemoI ...

  7. java(IO)读写文件乱码转换UTF-8问题

    java(IO)读写文件乱码转换UTF-8问题 读取文件 String Content = ""; // 文件很长的话建议使用StringBuffer try { FileInpu ...

  8. Java:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  9. IO流----操作文件的9种方法代码实现

    IO流----操作文件的9种方法代码实现: 1:使用字节流读写数据: 四种方式: method1:          每次读写一个字节,边读边写: /* * 复制文本文件. * * 数据源:从哪里来 ...

随机推荐

  1. React组件生命周期-初始化阶段的函数执行顺序

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  2. IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践

    原文:IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践 最近把编辑器换成IntelliJ IDEA,主要是Ecli ...

  3. 299. Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  4. Android基础之用Eclipse搭建Android开发环境和创建第一个Android项目(Windows平台)

    一.搭建Android开发环境 准备工作:下载Eclipse.JDK.Android SDK.ADT插件 下载地址:Eclipse:http://www.eclipse.org/downloads/ ...

  5. eclipse 中使用tomcat

    最近写了个商品搜索模块,要做成tomcat服务,以前只关注算法,从来没有使用过tomcat,这次上网上查了些资料还搞定(小公司真是锻炼人啊,以前我从来不考虑这些服务问题). 1.tomcat 环境的搭 ...

  6. 16_采用SharedPreferences保存用户偏好设置参数

    按钮事件 <Button android:id="@+id/button" android:layout_width="wrap_content" and ...

  7. C# 设计基础(一)

    (一)   C#项目的组成结构 项目结构 .config ---配置文件(存放配置参数文件) .csproj ---项目文件(管理文件项) .sln   ---解决方案文件(管理项目) .cs   - ...

  8. perl install module as non-root user

    install to local directory. 1. cpan 初始化,不用local::lib,mannual就行,其他auto2. 修改cpan 配置文件 cpan > o conf ...

  9. 由反汇编C程序来理解计算机是如何工作的

    C语言代码 int g(int x) { return x + 109; } int f(int x) { return g(x); } int main() { return f(122) + 3; ...

  10. 如何实现上下左右键盘控制焦点使之落在相邻文本框或下拉框中-Web开发/JavaScript

    我用jquery只实现了文本框的移动(暂时上下移动等同于左右移动) $(function () { var cols = 1;//按一下跳几个控件 var obj = $("input[id ...