项目需要进行大文件的读写,调查测试的结果使我决定使用MappedByteBuffer及相关类进行文件的操作,效果不是一般的高。

网上参考资源很多,如下两篇非常不错:

1、花1K内存实现高效I/O的RandomAccessFile类
2、Java中Stream和Memory-mapped File的I/O性能对比
小结:
1、RandomAccessFile本身不带缓冲读写,和FileInputStream、FileOutputStream等一样,直接按字节读写时,性能不可接受;
2、使用MappedByteBuffer读写,固然性能会得到极大提升;其实只要自己处理缓冲,性能都会有非常大的提升,比如以下两种方式(见下记代码)中第一种使用了MappedByteBuffer,第二种自己进行缓冲,对于12M的文件,后者的效率甚至高于前者。
3、BufferedXXXX之类的缓冲流,如果仅使用默认的buffer size,性能不一定最优,要权衡不同情况各种因素设置大小。
package helloword.helloword;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel; public class Test { public static void main(String[] args) {
// TODO Auto-generated method stub } public void test() throws IOException {
/*
* 测试结果与Buffer size有关
*/
// 1、使用MappedByteBuffer: 0.7s
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
RandomAccessFile rafi = new RandomAccessFile(srcFile, "r");
RandomAccessFile rafo = new RandomAccessFile(destFile, "rw");
FileChannel fci = rafi.getChannel();
FileChannel fco = rafo.getChannel();
long size = fci.size();
MappedByteBuffer mbbi = fci.map(FileChannel.MapMode.READ_ONLY, 0, size);
MappedByteBuffer mbbo = fco.map(FileChannel.MapMode.READ_WRITE, 0, size);
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
byte b = mbbi.get(i);
mbbo.put(i, b);
}
fci.close();
fco.close();
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s"); } public void test2() throws IOException {
// 2、自己处理Buffer(RandomAccessFile): 0.13s
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
RandomAccessFile rafi = new RandomAccessFile(srcFile, "r");
RandomAccessFile rafo = new RandomAccessFile(destFile, "rw"); byte[] buf = new byte[1024 * 8]; long start = System.currentTimeMillis(); int c = rafi.read(buf); while (c > 0) {
if (c == buf.length) {
rafo.write(buf);
} else {
rafo.write(buf, 0, c);
} c = rafi.read(buf);
}
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s"); } public void test3() throws IOException {
// 3、BufferedInputStream&BufferedOutputStream: 3.02s
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
FileInputStream rafi = new FileInputStream(srcFile);
FileOutputStream rafo = new FileOutputStream(destFile); BufferedInputStream bis = new BufferedInputStream(rafi, 8192);
BufferedOutputStream bos = new BufferedOutputStream(rafo, 8192);
long size = rafi.available(); long start = System.currentTimeMillis(); for (int i = 0; i < size; i++) {
byte b = (byte) bis.read();
bos.write(b);
}
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s");
}
}

RandomAcessFile、MappedByteBuffer和缓冲读/写文件的更多相关文章

  1. java读/写文件

    读取文件参考:https://blog.csdn.net/weixin_42129373/article/details/82154471 写入文件参考:https://blog.csdn.net/B ...

  2. C++ 二进制文件 读 写文件

    1 #include <iostream> 2 #include <string> 3 #include<fstream> 4 using namespace st ...

  3. read(),write() 读/写文件

    read read()是一个系统调用函数.用来从一个文件中,读取指定长度的数据到 buf 中. 使用read()时需要包含的头文件: <unistd.h> 函数原型: ssize_t re ...

  4. 关于使用 Java 分片读\写文件

    分片读取文件方法: /** * 分片读取文件块 * * @param path 文件路径 * @param position 角标 * @param blockSize 文件块大小 * @return ...

  5. c# 读/写文件(各种格式)

    最简单的: --------写 //content是要写入文本的字符串 //(@txtPath + @"\" + rid + ".txt");要被写入的TXT ...

  6. Java基础之写文件——通过缓冲流写文件(StreamOutputToFile)

    控制台程序,生成一些二进制整型值并且将它们写入到文件中. import java.nio.file.*; import java.nio.*; import java.io.*; public cla ...

  7. Pandas 基础(4) - 读/写 Excel 和 CSV 文件

    这一节将分别介绍读/写 Excel 和 CSV 文件的各种方式: - 读入 CSV 文件 首先是准备一个 csv 文件, 这里我用的是 stock_data.csv, 文件我已上传, 大家可以直接下载 ...

  8. 【练习】Java中的读文件,文件的创建,写文件

    前言 大家好,给大家带来Java中的读文件,文件的创建,写文件的概述,希望你们喜欢 读文件 public static void read(String path,String filename){ ...

  9. py库: xlwt 、xlrd (写读EXCEL文件)

    写EXCEL文件 # -*- coding: utf-8 -*- import xlwt book = xlwt.Workbook(encoding = "utf-8", styl ...

随机推荐

  1. 模拟登陆115网盘(MFC版)

    [cpp] view plain copy // 模拟登陆115网盘 #include <afxinet.h> // 包含相关的头文件 /* 用抓包工具抓包可得到需要提交的数据,然后模拟提 ...

  2. Linux命令-dd

    dd命令用于指定大小的拷贝的文件或指定转换文件. 以下命令作用:将光驱设备拷贝成镜像文件 参数 if 输入的文件名称 参数 of 输出的文件名称 [root@localhost testA]# dd ...

  3. HTML5塔防游戏——《三国塔防》 - Yorhom's Game Box

    h3{ font-size:20px; } HTML5塔防游戏--<三国塔防> 游戏介绍: <三国塔防>是一款基于HTML5和Javascript的2D塔防游戏.游戏中除了塔防 ...

  4. adobeMediaServer

    参考地址: http://www.piis.cn/news/new519.asp http://www.cuplayer.com/player/PlayerCode/RTMP/2012/0918427 ...

  5. 短信发送AZDG加密算法

    public static string passport_encrypt(string txt, string key)         {             //   使用随机数发生器产生  ...

  6. js调用高德API获取所在当前城市

    可以在js代码中直接调用API接口,获取所处当前城市信息,代码如下: <script type="text/javascript"> function getCurre ...

  7. YTU 2616: A代码完善--简易二元运算

    2616: A代码完善--简易二元运算 时间限制: 1 Sec  内存限制: 128 MB 提交: 280  解决: 187 题目描述 注:本题只需要提交填写部分的代码,请按照C++方式提交. 编写二 ...

  8. shell查找匹配行,输出该行并输出下面的一行

    查找匹配行,输出该行并输出下面的一行 grep: grep -A 1 'keyword'   file awk:awk '$0~/keyword/{print $0; getline; print $ ...

  9. JS里面匿名函数的调用 & 变量作用域的实验

    参考 http://www.educity.cn/wenda/54753.html 已实验验证结果正确. 1.下列哪些正确?(B.C) A.function(){ alert("Here!& ...

  10. config windows virtual machine on mac

    1.download virtualbox  and related extension pack from http://www.oracle.com/technetwork/server-stor ...