Search code examples
javaarraylistfilewriterlarge-data

Writing huge ArrayList to file in a human readable format


I have a program that processes quite a lot of sensor data from a sensor system. I'm currently looking into writing the output from my program to a text file so that I can check if it is processes properly from the program.

Right now I am writing a few identifiers before the ArrayList and then writing the ArrayList to the file using ArrayList.toString().

lineToWrite = identifer1 + ";" + identifier2 + ";" + ArrayList.toString()

The output file contains 21 lines in total, and the ArrayLists are from 100 items to 400.000 items large. Using the toString() method makes it impossible for any of the file editing programs I usually use to open the file and inspect them.

I thought of doing a small processing of the items in the ArrayList:

String lineToWrite = "";

String arrayListString = "\n";
for(String s : sensorLine){
    arrayListString += "\t" + s + "\n";
}

lineToWrite = identifer1 + ";" + identifier2 + ";" + arrayListString;

but it seems like this takes forever for some of the ArrayLists which are large enough. Does anyone have a better/faster approach for doing this or know of a good file viewing program?

I have used the following, which don't have the following problems:

  • Notepad++ -> Slow to open and laggy once fully opened
  • Sublime Text 3 -> Very slow to open!

As a small side note to the sensor data: I have in total 2.3 million sensor inputs.

EDIT1:

To extend the problem question I might have to add that it is the part of splitting the enormous array into a single string that proved to be a problem. The program iterates very slowly over the array as it is just increasing the size of the arrayListString on every pass through and that takes up a lot of memory/processing power I guess.

EDIT2:

As for the writing method itself I am using a BufferedWriter(), with placeholders for the actual method variables:

output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename, toAppend), "UTF-8"));

And for the actual writing I am using:

output.append(line)
output.flush();

Solution

  • In the end I found a solution.

    I used a StringBuilder to surpass the problem of writing a huge string to the file. The approach is as follows:

    StringBuilder sb = new StringBuilder();
    for(String s : arrayList){
        sb.append("\t" + s + "\n"
    }
    
    String line = identifier1 + ";" + identfier2 + ";" + sb.toString();
    

    And for the editor Sublime Text 3 didn't seem to mind too much as long as the lines weren't 400.000 characters long