Java 大文件IO操作效率对比【我说说 你瞅瞅】
Java 文件IO操作效率对比
注:本文只做时间消耗层面对比,内存占用层面需要特别关注!
1. 参数说明
文件总大小:2,111,993,850 字节(2.11 GB)
static String defaultFilePath = "/tmp/data-24081412.json";
缓冲区大小:8192
static int defaultByteLength = 1024 * 8;
2. 示例介绍
通过以下几种方式读取数据文件,并连续进行 10 次测试:
- FileInputStream + byte[] 文件字节输入流 + 字节数组读取方式
- FileInputStream + Scanner 文件字节输入流 + Scanner 读取方式
- FileReader + char[] 文件字符输入流 + 字符数组方式
- BufferedReader 缓冲字符流方式
- FileChannel 文件输入输出管道流「NIO」
3. 比对结果
「5. FileChannel」 > 「1. FileInputStream + byte[]」> 「3. FileReader + char[]」>= 「4. BufferedReader」 > 「2. FileInputStream + Scanner」
在操作文件时,会将文件区分为大文件、小文件、文本文件、二进制文件等等吗,根据不同的文件需要选择合适的读取方式。通常大文件推荐使用 「FileChannel」效率会更高,小文件采用 IO 或 NIO 都可以。
4. 示例代码
4.1. FileInputStream + byte[] 文件字节输入流 + 字节数组读取方式
/**
* FileInputStream + byte[] 方式
* 等同于 BufferedInputStream 字节输入缓冲流
* 文件字节输入流 + 字节数组读取方式
* 适用于:二进制文件或非文本文件
*/
public void testFileInputStreamWithBytes() {
long startTime = new Date().getTime();
// 使用 try 包装
try (FileInputStream fileInputStream = new FileInputStream(defaultFilePath)){
byte[] reads = new byte[defaultByteLength];
int readCount;
while ((readCount = fileInputStream.read(reads)) != -1) {
// TODO 处理数据
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("FileInputStream + byte[] 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
10 次测试结果如下
FileInputStream + byte[] 方式 读取文件共使用 884 毫秒
FileInputStream + byte[] 方式 读取文件共使用 331 毫秒
FileInputStream + byte[] 方式 读取文件共使用 319 毫秒
FileInputStream + byte[] 方式 读取文件共使用 420 毫秒
FileInputStream + byte[] 方式 读取文件共使用 333 毫秒
FileInputStream + byte[] 方式 读取文件共使用 321 毫秒
FileInputStream + byte[] 方式 读取文件共使用 327 毫秒
FileInputStream + byte[] 方式 读取文件共使用 339 毫秒
FileInputStream + byte[] 方式 读取文件共使用 328 毫秒
FileInputStream + byte[] 方式 读取文件共使用 398 毫秒
4.2. FileInputStream + Scanner 文件字节输入流 + Scanner 读取方式
/**
* FileInputStream + Scanner 方式
* 文件字节输入流 + Scanner 读取文本方式
* 适用于:文本文件
*/
public void testFileInputStreamWithScanner() {
long startTime = new Date().getTime();
// 使用 try 包装
try (FileInputStream fileInputStream = new FileInputStream(defaultFilePath)){
Scanner scanner = new Scanner(fileInputStream);
while (scanner.hasNext()) {
scanner.nextLine();
// TODO 处理数据
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("FileInputStream + Scanner 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
10 次测试结果如下
没有缓冲区,性能急剧下降!!
FileInputStream + Scanner 方式 读取文件共使用 16755 毫秒
FileInputStream + Scanner 方式 读取文件共使用 18744 毫秒
FileInputStream + Scanner 方式 读取文件共使用 17929 毫秒
FileInputStream + Scanner 方式 读取文件共使用 18640 毫秒
FileInputStream + Scanner 方式 读取文件共使用 18316 毫秒
FileInputStream + Scanner 方式 读取文件共使用 18015 毫秒
FileInputStream + Scanner 方式 读取文件共使用 18479 毫秒
FileInputStream + Scanner 方式 读取文件共使用 18755 毫秒
FileInputStream + Scanner 方式 读取文件共使用 18907 毫秒
FileInputStream + Scanner 方式 读取文件共使用 18783 毫秒
4.3. FileReader + char[] 文件字符输入流 + 字符数组方式
/**
* FileReader + char[] 方式
* 等同于 BufferedReader 字符输入缓冲流
* 文件字符输入流 + 字符数组方式
* 适用于:字符文件
*/
public void testFileReaderWithChars() {
long startTime = new Date().getTime();
// 使用 try 包装
try (FileReader fileReader = new FileReader(defaultFilePath)){
char[] reads = new char[defaultByteLength];
int readCount;
while ((readCount = fileReader.read(reads)) != -1) {
// TODO 处理数据
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("FileReader + char[] 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
10 次测试结果如下
FileReader + char[] 方式 读取文件共使用 922 毫秒
FileReader + char[] 方式 读取文件共使用 971 毫秒
FileReader + char[] 方式 读取文件共使用 842 毫秒
FileReader + char[] 方式 读取文件共使用 985 毫秒
FileReader + char[] 方式 读取文件共使用 868 毫秒
FileReader + char[] 方式 读取文件共使用 1207 毫秒
FileReader + char[] 方式 读取文件共使用 1031 毫秒
FileReader + char[] 方式 读取文件共使用 981 毫秒
FileReader + char[] 方式 读取文件共使用 1259 毫秒
FileReader + char[] 方式 读取文件共使用 1034 毫秒
4.4. BufferedReader 缓冲字符流方式
/**
* BufferedReader 方式
* 缓冲字符流方式
* 适用于:字符文件
*/
public void testBufferedReader() {
long startTime = new Date().getTime();
// 使用 try 包装
try (BufferedReader fileReader = new BufferedReader(new FileReader(defaultFilePath))){
String line;
while ((line = fileReader.readLine()) != null) {
// TODO 处理数据
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("BufferedReader 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
10 次测试结果如下
BufferedReader 方式 读取文件共使用 1870 毫秒
BufferedReader 方式 读取文件共使用 1895 毫秒
BufferedReader 方式 读取文件共使用 1890 毫秒
BufferedReader 方式 读取文件共使用 1875 毫秒
BufferedReader 方式 读取文件共使用 1829 毫秒
BufferedReader 方式 读取文件共使用 2060 毫秒
BufferedReader 方式 读取文件共使用 1821 毫秒
BufferedReader 方式 读取文件共使用 1944 毫秒
BufferedReader 方式 读取文件共使用 1902 毫秒
BufferedReader 方式 读取文件共使用 1860 毫秒
4.5. FileChannel 文件输入输出管道流「NIO」
/**
* FileChannel 方式
* 文件输入输出管道流
*/
public void testFileChannel() {
long startTime = new Date().getTime();
// 使用 try 包装
try (FileChannel channel = FileChannel.open(Paths.get(defaultFilePath), StandardOpenOption.READ)){
// 构造B yteBuffer 有两个方法,ByteBuffer.allocate和 ByteBuffer.allocateDirect,两个方法都是相同入参,含义却不同。
// ByteBuffer.allocate(capacity) 分配的是非直接缓冲区,非直接缓冲区的操作会在Java堆内存中进行,数据的读写会通过Java堆内存来传递。
// ByteBuffer.allocateDirect(capacity) 分配的是直接缓冲区, 直接缓冲区的操作可以通过本地I/O传递,避免了在Java堆和本地堆之间的数据传输。
ByteBuffer buf = ByteBuffer.allocate(defaultByteLength);
while (channel.read(buf) != -1) {
buf.flip();
// TODO 处理数据
// System.out.println(new String(buf.array()));
buf.clear();
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("FileChannel 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
10 次测试结果如下
FileChannel 方式 读取文件共使用 314 毫秒
FileChannel 方式 读取文件共使用 293 毫秒
FileChannel 方式 读取文件共使用 332 毫秒
FileChannel 方式 读取文件共使用 296 毫秒
FileChannel 方式 读取文件共使用 285 毫秒
FileChannel 方式 读取文件共使用 290 毫秒
FileChannel 方式 读取文件共使用 283 毫秒
FileChannel 方式 读取文件共使用 282 毫秒
FileChannel 方式 读取文件共使用 298 毫秒
FileChannel 方式 读取文件共使用 280 毫秒
5. 结语
在 Java 8 中,「FileChannel」是处理文件 I/O 的高效方式,相比于传统的 I/O流「FileInputStream」、「FileOutputStream」等更加灵活方便且效率更高。
6. 代码附录
package com.demo.io;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Date;
import java.util.Scanner;
/**
* 文件读取
* @date 2024-08-14 16:42:21
*/
public class FileReadTest {
// 文件路径
static String defaultFilePath = "/Users/changbeibei/Desktop/work/wfilep.log-24081412";
// 8k
static int defaultByteLength = 1024 * 8;
/**
* FileInputStream + byte[] 方式
* 等同于 BufferedInputStream 字节输入缓冲流
* 文件字节输入流 + 字节数组读取方式
* 适用于:二进制文件或非文本文件
*/
@Test
public void testFileInputStreamWithBytes() {
long startTime = new Date().getTime();
// 使用 try 包装
try (FileInputStream fileInputStream = new FileInputStream(defaultFilePath)){
byte[] reads = new byte[defaultByteLength];
int readCount;
while ((readCount = fileInputStream.read(reads)) != -1) {
// TODO 处理数据
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("FileInputStream + byte[] 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
/**
* FileInputStream + Scanner 方式
* 文件字节输入流 + Scanner 读取文本方式
* 适用于:文本文件
*/
@Test
public void testFileInputStreamWithScanner() {
long startTime = new Date().getTime();
// 使用 try 包装
try (FileInputStream fileInputStream = new FileInputStream(defaultFilePath)){
Scanner scanner = new Scanner(fileInputStream);
while (scanner.hasNext()) {
scanner.nextLine();
// TODO 处理数据
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("FileInputStream + Scanner 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
/**
* FileReader + char[] 方式
* 等同于 BufferedReader 字符输入缓冲流
* 文件字符输入流 + 字符数组方式
* 适用于:字符文件
*/
@Test
public void testFileReaderWithChars() {
long startTime = new Date().getTime();
// 使用 try 包装
try (FileReader fileReader = new FileReader(defaultFilePath)){
char[] reads = new char[defaultByteLength];
int readCount;
while ((readCount = fileReader.read(reads)) != -1) {
// TODO 处理数据
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("FileReader + char[] 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
/**
* BufferedReader 方式
* 缓冲字符流方式
* 适用于:字符文件
*/
@Test
public void testBufferedReader() {
long startTime = new Date().getTime();
// 使用 try 包装
try (BufferedReader fileReader = new BufferedReader(new FileReader(defaultFilePath))){
String line;
while ((line = fileReader.readLine()) != null) {
// TODO 处理数据
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("BufferedReader 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
/**
* FileChannel 方式
* 文件输入输出管道流
*/
@Test
public void testFileChannel() {
long startTime = new Date().getTime();
// 使用 try 包装
try (FileChannel channel = FileChannel.open(Paths.get(defaultFilePath), StandardOpenOption.READ)){
// 构造B yteBuffer 有两个方法,ByteBuffer.allocate和 ByteBuffer.allocateDirect,两个方法都是相同入参,含义却不同。
// ByteBuffer.allocate(capacity) 分配的是非直接缓冲区,非直接缓冲区的操作会在Java堆内存中进行,数据的读写会通过Java堆内存来传递。
// ByteBuffer.allocateDirect(capacity) 分配的是直接缓冲区, 直接缓冲区的操作可以通过本地I/O传递,避免了在Java堆和本地堆之间的数据传输。
ByteBuffer buf = ByteBuffer.allocate(defaultByteLength);
while (channel.read(buf) != -1) {
buf.flip();
// TODO 处理数据
// System.out.println(new String(buf.array()));
buf.clear();
}
} catch (IOException e) {
System.out.printf("读取文件异常[ %s ]%n", e.getMessage());
}
System.out.printf("FileChannel 方式 读取文件共使用 %d 毫秒%n", new Date().getTime() - startTime);
}
@Test
public void testMain() {
for (int i = 0; i < 10; i++) {
System.out.printf("第 %d 次测试%n", i + 1);
testFileInputStreamWithBytes();
testFileInputStreamWithScanner();
testFileReaderWithChars();
testBufferedReader();
testFileChannel();
}
}
}
Java 大文件IO操作效率对比【我说说 你瞅瞅】的更多相关文章
- Java:大文件拆分工具
java大文件拆分工具(过滤掉表头) import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...
- java 大文件上传 断点续传 完整版实例 (Socket、IO流)
ava两台服务器之间,大文件上传(续传),采用了Socket通信机制以及JavaIO流两个技术点,具体思路如下: 实现思路: 1.服:利用ServerSocket搭建服务器,开启相应端口,进行长连接操 ...
- java 大文件分割与组装
不多说,直接上代码 1 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; im ...
- java大文件断点续传
java两台服务器之间,大文件上传(续传),采用了Socket通信机制以及JavaIO流两个技术点,具体思路如下: 实现思路:1.服:利用ServerSocket搭建服务器,开启相应端口,进行长连接操 ...
- java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
java处理大文件,一般用BufferedReader,BufferedInputStream这类带缓冲的Io类,不过如果文件超大的话,更快的方式是采用MappedByteBuffer. Mapped ...
- Java 之文件IO编程 之写入
package com.sun; /* * 操作对文件IO的写 * 2014-08-10 */ import java.io.*; public class File_Write { public s ...
- Java 之文件IO编程 之读取
package com.sun; /* * 这里是对文件IO流读取的操作 * 2014-08-10 */ import java.io.*; public class File_test { publ ...
- java 导出 excel 最佳实践,java 大文件 excel 避免OOM(内存溢出) excel 工具框架
产品需求 产品经理需要导出一个页面的所有的信息到 EXCEL 文件. 需求分析 对于 excel 导出,是一个很常见的需求. 最常见的解决方案就是使用 poi 直接同步导出一个 excel 文件. 客 ...
- java 流 文件 IO
Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...
- java大文件上传解决方案
最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...
随机推荐
- JS模拟循环批量请求后台接口
使用async, await处理异步请求.用Promise, setTimeout函数模拟后台接口 <!DOCTYPE html> <html> <script type ...
- ansible v2.9.9离线安装脚本
链接:https://pan.baidu.com/s/18uxyWWyJ39i1mJJ1hb8zww?pwd=QWSC 提取码:QWSC
- 从0到1:CTFer成长之路网上平台的使用以及docker环境配置
1.首先安装docker(这里在kali里演示) sudo su #以root身份运行 apt install docker.io #安装docker systemctl start docker # ...
- libevent之evbuffer
目录 Evbuffers:缓冲 IO 的实用程序功能 简介 创建或释放 evbuffer Evbuffers 和线程安全 检查 evbuffer 向 evbuffer 添加数据:基础知识 将数据从一个 ...
- bootstrap-sweetalert
官网地址:http://lipis.github.io/bootstrap-sweetalert/ 模板样式 // 删除书籍,甜甜的警告 $('.bookdel').click(function () ...
- 网络OSI七层模型及各层作用 tcp-ip
背景 虽然说以前学习计算机网络的时候,学过了,但为了更好地学习一些物联网协议(MQTT.CoAP.LWM2M.OPC),需要重新复习一下. OSI七层模型 七层模型,亦称OSI(Open System ...
- css 手稿
CSS 网页的布局和外观的显示样式 没有一张图解决不了的事:https://www.processon.com/mindmap/5e368a8be4b0d27af184e118 选择器 * 通配符选择 ...
- 自己理解的TCP三次握手
### TCP 三次握手过程是怎样的? TCP的建立连接是通过三次握手来进行的.三次握手的过程如下图: 说实话这个很好理解,我称之为N字型 首先我们理解到建立连接是一个虚的概念了对吧?那么我们来设计一 ...
- 解决方案 | Adobe Acrobat XI Pro 右键菜单“在Acrobat中合并文件”丢失的最佳修复方法
1.问题 Adobe Acrobat XI Pro右键菜单"转换为Adobe PDF"与"在Acrobat中合并文件" 不见了. 2.解决方案 桌面左下角搜索& ...
- [oeasy]python0045_四种进制_binary_octal_decimal_hexadecimal
四种进制 回忆上次内容 上次研究了 通过 八进制数值 转义 \ooo 把(ooo)8进制对应的ascii字符输出 转义序列 \n.\t 是 转义序列 \xhh 也是 转义序列 \ooo 还是 转义序列 ...