在java中将整个列表写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21040854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:09:29  来源:igfitidea点击:

Writing an entire list to a file in java

javafilearraylistiojava-io

提问by Ahmar Ali

I am working on a big project where I have more than 1 million lines of data. Data is divided into various files containing 20,000 lines each. Now the data from each file is read line by line and some variable x is concatenated to each line. I am storing these concatenated string to an array list. Then this array list is saved to output files line by line.

我正在做一个大项目,我有超过 100 万行数据。数据被分成不同的文件,每个文件包含 20,000 行。现在逐行读取每个文件中的数据,并将一些变量 x 连接到每一行。我将这些连接的字符串存储到数组列表中。然后将这个数组列表逐行保存到输出文件中。

This is taking 3-4 minutes on each file. Is there anyway to write the entire ArrayListto a file in one go, so that it won't take that much time. Or is there any faster way to do this?

每个文件需要 3-4 分钟。无论如何,是否可以ArrayList一次性将整个文件写入文件,这样就不会花费太多时间。或者有没有更快的方法来做到这一点?

Here is some sample code:

下面是一些示例代码:

    List<String> outputData = new ArrayList<String>(); 
//Output arraylist containing concatenated data

writeLines(File outputFile,outputData); //The data is written to file

What would be the fastest way to achieve this task?

完成这项任务的最快方法是什么?

采纳答案by JHS

Once you have the ArrayListready you can use the writeLinesmethod from FileUtilsto write the entire ArrayListin one go.

一旦你ArrayList准备好了,你就可以使用writeLinesfrom的方法FileUtils一口气写出整个ArrayList

Have a look at the documentation hereand the various writeLinesmethods that are available.

查看此处的文档以及writeLines可用的各种方法。

回答by Moritz Petersen

A proper solution could be to skip the ArrayListand write directly to file. But you should consider, that disk IO is way slower than RAM.

一个适当的解决方案可能是跳过ArrayList并直接写入文件。但是您应该考虑,磁盘 IO 比 RAM 慢得多。

Testing like this:

像这样测试:

    Collection<String> list = new ArrayList<String>();
    for (int i = 0; i < 1000000; i++) {
        // just fill something in:
        list.add("A " + i + " " + new Date() + "!");
    }
    long start = System.nanoTime();
    PrintWriter out = new PrintWriter("example.out");
    for (String line : list) {
        out.println(line);
    }
    out.close();
    long end = System.nanoTime();
    System.out.println((end - start) / 1000000000D + " sec");

Prints on my old Dell laptop:

在我的旧戴尔笔记本电脑上打印:

0.508509454 sec

回答by Ahmar Ali

First I was using writeStringtoFile to write individual lines to file which took ages. Seems like first saving all lines in array list and writing whole list with writeLines function solved the problem. Now it only takes second.

首先,我使用 writeStringtoFile 将单独的行写入文件,这需要很长时间。似乎首先保存数组列表中的所有行并使用 writeLines 函数写入整个列表解决了这个问题。现在只需要第二个。

Thanks everyone for helping

感谢大家的帮助

Ahmar

艾哈迈尔