Search code examples
javafile-ioguava

How to write an ImmutableList<Number> to a file?


This should be pretty simple, but it takes about a minute to write about 40 MB. There's got to be a better way. I imagine there's a lot of hangup on casting the Number to a double, but I'm not sure if that's where the problem lies. What's the fastest way to write an ImmutableList<Number> to a binary file of doubles?

public static void multiDoubleToImgFastForSO(String outFilename, ImmutableList<ImmutableList<Number>> arrays) {
        FileOutputStream fos = null;
        DataOutputStream dos = null;
        
        try {
            fos = new FileOutputStream(outFilename);
            dos = new DataOutputStream(fos);
            
            for (ImmutableList<Number> array : arrays) {
                
                for (Number number : array) {
                    dos.writeDouble(number.doubleValue());
                }
            }
            
            dos.close();
            fos.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

Solution

  • When working with large amounts of data, it is recommended to use buffered i/o streams, so the following update should increase performance.

    Also, the code snippet above needs to use try-with-resources

    try (DataOutputStream dos = new DataOutputStream(
        new BufferedOutputStream(new FileOutputStream(outputFilename), 16_000)
    )) {
        for (ImmutableList<Number> array : arrays) {
            for (Number number : array) {
                dos.writeDouble(number.doubleValue());
            }
        }
    }