在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
Writing an entire list to a file in java
提问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 ArrayList
to 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 ArrayList
ready you can use the writeLines
method from FileUtils
to write the entire ArrayList
in one go.
一旦你ArrayList
准备好了,你就可以使用writeLines
from的方法FileUtils
一口气写出整个ArrayList
。
Have a look at the documentation hereand the various writeLines
methods that are available.
查看此处的文档以及writeLines
可用的各种方法。
回答by Moritz Petersen
A proper solution could be to skip the ArrayList
and 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
艾哈迈尔