java 逐行读写文件_在Java中逐行读取和写入大文件的最快方式

本文通过代码示例展示了在Java中如何高效地逐行读取和写入大文件,探讨了硬件限制对性能的影响,并提供了不同文件大小的测试结果,揭示了内存和CPU在文件操作中的作用。
摘要由CSDN通过智能技术生成

我怀疑你的真正问题是你的硬件有限,你所做的是软件不会有太大的不同.如果你有足够的内存和CPU,更高级的技巧可以帮助,但如果你只是等待硬盘驱动器,因为该文件没有缓存,这将不会有太大的不同.

BTW:10秒或500 MB /秒的500 MB是HDD的典型读取速度.

尝试运行以下操作以查看系统无法高效缓存文件的位置.

public static void main(String... args) throws IOException {

for (int mb : new int[]{50, 100, 250, 500, 1000, 2000})

testFileSize(mb);

}

private static void testFileSize(int mb) throws IOException {

File file = File.createTempFile("test", ".txt");

file.deleteOnExit();

char[] chars = new char[1024];

Arrays.fill(chars, 'A');

String longLine = new String(chars);

long start1 = System.nanoTime();

PrintWriter pw = new PrintWriter(new FileWriter(file));

for (int i = 0; i < mb * 1024; i++)

pw.println(longLine);

pw.close();

long time1 = System.nanoTime() - start1;

System.out.printf("Took %.3f seconds to write to a %d MB, file rate: %.1f MB/s%n",

time1 / 1e9, file.length() >> 20, file.length() * 1000.0 / time1);

long start2 = System.nanoTime();

BufferedReader br = new BufferedReader(new FileReader(file));

for (String line; (line = br.readLine()) != null; ) {

}

br.close();

long time2 = System.nanoTime() - start2;

System.out.printf("Took %.3f seconds to read to a %d MB file, rate: %.1f MB/s%n",

time2 / 1e9, file.length() >> 20, file.length() * 1000.0 / time2);

file.delete();

}

在具有大量内存的Linux机器上.

Took 0.395 seconds to write to a 50 MB, file rate: 133.0 MB/s

Took 0.375 seconds to read to a 50 MB file, rate: 140.0 MB/s

Took 0.669 seconds to write to a 100 MB, file rate: 156.9 MB/s

Took 0.569 seconds to read to a 100 MB file, rate: 184.6 MB/s

Took 1.585 seconds to write to a 250 MB, file rate: 165.5 MB/s

Took 1.274 seconds to read to a 250 MB file, rate: 206.0 MB/s

Took 2.513 seconds to write to a 500 MB, file rate: 208.8 MB/s

Took 2.332 seconds to read to a 500 MB file, rate: 225.1 MB/s

Took 5.094 seconds to write to a 1000 MB, file rate: 206.0 MB/s

Took 5.041 seconds to read to a 1000 MB file, rate: 208.2 MB/s

Took 11.509 seconds to write to a 2001 MB, file rate: 182.4 MB/s

Took 9.681 seconds to read to a 2001 MB file, rate: 216.8 MB/s

在具有大量内存的Windows机器上.

Took 0.376 seconds to write to a 50 MB, file rate: 139.7 MB/s

Took 0.401 seconds to read to a 50 MB file, rate: 131.1 MB/s

Took 0.517 seconds to write to a 100 MB, file rate: 203.1 MB/s

Took 0.520 seconds to read to a 100 MB file, rate: 201.9 MB/s

Took 1.344 seconds to write to a 250 MB, file rate: 195.4 MB/s

Took 1.387 seconds to read to a 250 MB file, rate: 189.4 MB/s

Took 2.368 seconds to write to a 500 MB, file rate: 221.8 MB/s

Took 2.454 seconds to read to a 500 MB file, rate: 214.1 MB/s

Took 4.985 seconds to write to a 1001 MB, file rate: 210.7 MB/s

Took 5.132 seconds to read to a 1001 MB file, rate: 204.7 MB/s

Took 10.276 seconds to write to a 2003 MB, file rate: 204.5 MB/s

Took 9.964 seconds to read to a 2003 MB file, rate: 210.9 MB/s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值