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

2022-09-02 03:27:00

我一直在寻找最快的方法,以最快的方式在Java中再次读取和写入内存有限(约64MB)的大文件(0.5 - 1 GB)。文件中的每一行都代表一条记录,因此我需要逐行获取它们。该文件是普通文本文件。

我尝试了BufferedReader和BufferedWriter,但它似乎不是最好的选择。读取和写入大小为 0.5 GB 的文件大约需要 35 秒,只有读写,无需处理。我认为这里的瓶颈是写作,因为仅阅读就需要大约10秒钟。

我试图读取字节数组,但随后在每个已读取的数组中搜索行需要更多时间。

有什么建议吗?谢谢


答案 1

我怀疑你真正的问题是你的硬件有限,你所做的是软件不会有太大的区别。如果您有足够的内存和CPU,则更高级的技巧可以提供帮助,但是如果您只是因为文件未缓存而在硬盘驱动器上等待,则不会有太大区别。

顺便说一句:10 秒内 500 MB 或 50 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

在具有大量内存的窗口机器上。

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

答案 2

我要尝试的第一件事是增加 BufferedReader 和 BufferedWriter 的缓冲区大小。默认缓冲区大小未记录在案,但至少在 Oracle VM 中,它们为 8192 个字符,这不会带来太大的性能优势。

如果您只需要创建文件的副本(并且不需要实际访问数据),我会放弃读取器/写入器方法,并使用字节数组作为缓冲区直接使用 InputStream 和 OutputStream:

FileInputStream fis = new FileInputStream("d:/test.txt");
FileOutputStream fos = new FileOutputStream("d:/test2.txt");
byte[] b = new byte[bufferSize];
int r;
while ((r=fis.read(b))>=0) {
    fos.write(b, 0, r);         
}
fis.close();
fos.close();

或者实际使用NIO:

FileChannel in = new RandomAccessFile("d:/test.txt", "r").getChannel();
FileChannel out = new RandomAccessFile("d:/test2.txt", "rw").getChannel();
out.transferFrom(in, 0, Long.MAX_VALUE);
in.close();
out.close();

然而,在对不同的复制方法进行基准测试时,每次运行基准测试之间的差异(持续时间)比不同实现之间的差异(持续时间)要大得多。I / O缓存(在操作系统级别和硬盘缓存上)在这里起着重要作用,很难说什么是更快的。在我的硬件上,使用 BufferedReader 和 BufferedWriter 逐行复制 1GB 的文本文件在某些运行中花费的时间不到 5 秒,而在其他运行中则需要 30 多秒。


推荐