当前位置: 首页 > 面试题库 >

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

贝钧
2023-03-14
问题内容

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

我尝试了BufferedReader和BufferedWriter,但这似乎不是最好的选择。读写大小为0.5
GB的文件大约需要35秒钟,仅进行读写操作而不进行任何处理。我认为这里是写作的瓶颈,因为单独阅读大约需要10秒钟。

我尝试读取字节数组,但是在每个读取的数组中搜索行会花费更多时间。

有什么建议吗?谢谢


问题答案:

我怀疑您的真正问题是您的硬件有限,而您所做的只是软件不会带来太大变化。如果您有足够的内存和CPU,可以使用更高级的技巧,但是如果由于文件未缓存而仅在硬盘上等待,则不会有太大的不同。

BTW: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

在具有大量内存的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


 类似资料:
  • 问题内容: 我的文件中有1亿条记录,需要一种有效且最快的方法来从中的文件读取数组数组。 文件看起来像: 我想逐行读取此文件为: 首先阅读: 然后: 依此类推:’ 我如何读取这样的文件,我知道它看起来并不完全像文件,但是我需要以另存为JSON的这种格式读取该文件 问题答案: 您可以使用JSON Processing API(JSR 353) 来以流方式处理数据:

  • 问题内容: 如何使用框架有效地从大文件读取并将大数据写入文件。 我工作,并和曾尝试类似如下: 谁能告诉我,如果我的文件大小超过2 GB,我应该遵循相同的步骤吗? 如果大量的书面操作,我想在写作时做类似的事情,该怎么办? 问题答案: 请注意,您可以像示例代码那样简单地用于复制文件,只是速度更快,而且仅一行代码。 否则,如果您已经打开了两个文件通道,则可以使用 将该通道的全部内容传输到该通道。请注意,

  • 问题内容: 当给定一个MAX_BUFFER_SIZE的缓冲区以及一个远远超过该缓冲区的文件时,怎么办: 以MAX_BUFFER_SIZE的块读取文件? 尽快完成 我尝试使用NIO 和常规IO 事实证明, 常规IO在执行与NIO相同的操作时快约100倍 。我想念什么吗?这是预期的吗?有没有更快的方法来读取缓冲区块中的文件? 最终,我正在处理一个大文件,但我没有足够的内存来一次读取所有文件。相反,我想

  • 本文向大家介绍php逐行读取txt文件写入数组的方法 原创,包括了php逐行读取txt文件写入数组的方法 原创的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了php逐行读取txt文件写入数组的方法。分享给大家供大家参考。具体如下: 假设有user.txt文件如下: 逐行读取user.txt并写入数组的方法如下: 问题搞定! 希望本文所述对大家的php程序设计有所帮助。

  • 问题内容: 我需要使用Java逐行读取大约5-6 GB的大型文本文件。 我如何快速做到这一点? 问题答案: 常见的模式是使用 如果你假设没有字符编码,则可以更快地读取数据。例如ASCII-7,但差别不大。你处理数据的时间很可能会花费更长的时间。 一种不太常用的模式,可以避免line泄漏的范围。 在Java 8中,你可以执行

  • 本文向大家介绍C#逐行读取文件的方法,包括了C#逐行读取文件的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#逐行读取文件的方法。分享给大家供大家参考。具体如下: 这里使用C#逐行读取文件,对于大文件的读取非常有用。 希望本文所述对大家的C#程序设计有所帮助。