Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing an entire list to a file in java

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?

like image 466
Ahmar Ali Avatar asked Nov 01 '25 01:11

Ahmar Ali


1 Answers

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.

like image 106
JHS Avatar answered Nov 03 '25 17:11

JHS