Writing an entire list to a file in java
Once you have the ArrayList
ready you can use the writeLines
method from FileUtils
to write the entire ArrayList
in one go.
Have a look at the documentation here and the various writeLines
methods that are available.
Ahmar Ali
Updated on June 04, 2022Comments
-
Ahmar Ali almost 2 years
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.
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?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?
-
Ahmar Ali over 10 yearsHello thanks for answering but I used this solution also but it is way slower. Is there any faster way of file writing?
-
Moritz Petersen over 10 yearsHow long are the lines? I just generated a file with 1.000.000 lines and it took less than 1 sec.