Writing an entire list to a file in java

11,968

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.

Share:
11,968
Ahmar Ali
Author by

Ahmar Ali

Updated on June 04, 2022

Comments

  • Ahmar Ali
    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
    Ahmar Ali over 10 years
    Hello thanks for answering but I used this solution also but it is way slower. Is there any faster way of file writing?
  • Moritz Petersen
    Moritz Petersen over 10 years
    How long are the lines? I just generated a file with 1.000.000 lines and it took less than 1 sec.