Fastest way to write an array of integers to a file in Java?

前端 未结 6 488
小鲜肉
小鲜肉 2020-12-05 08:33

As the title says, I\'m looking for the fastest possible way to write integer arrays to files. The arrays will vary in size, and will realistically contain anywhere between

6条回答
  •  温柔的废话
    2020-12-05 09:06

    I would use FileChannel from the nio package and ByteBuffer. This approach seems (on my computer) give 2 to 4 times better write performance:

    Output from program:

    normal time: 2555
    faster time: 765
    

    This is the program:

    public class Test {
    
        public static void main(String[] args) throws IOException {
    
            // create a test buffer
            ByteBuffer buffer = createBuffer();
    
            long start = System.currentTimeMillis();
            {
                // do the first test (the normal way of writing files)
                normalToFile(new File("first"), buffer.asIntBuffer());
            }
            long middle = System.currentTimeMillis(); 
            {
                // use the faster nio stuff
                fasterToFile(new File("second"), buffer);
            }
            long done = System.currentTimeMillis();
    
            // print the result
            System.out.println("normal time: " + (middle - start));
            System.out.println("faster time: " + (done - middle));
        }
    
        private static void fasterToFile(File file, ByteBuffer buffer) 
        throws IOException {
    
            FileChannel fc = null;
    
            try {
    
                fc = new FileOutputStream(file).getChannel();
                fc.write(buffer);
    
            } finally {
    
                if (fc != null)
                    fc.close();
    
                buffer.rewind();
            }
        }
    
        private static void normalToFile(File file, IntBuffer buffer) 
        throws IOException {
    
            DataOutputStream writer = null;
    
            try {
                writer = 
                    new DataOutputStream(new BufferedOutputStream(
                            new FileOutputStream(file)));
    
                while (buffer.hasRemaining())
                    writer.writeInt(buffer.get());
    
            } finally {
                if (writer != null)
                    writer.close();
    
                buffer.rewind();
            }
        }
    
        private static ByteBuffer createBuffer() {
            ByteBuffer buffer = ByteBuffer.allocate(4 * 25000000);
            Random r = new Random(1);
    
            while (buffer.hasRemaining()) 
                buffer.putInt(r.nextInt());
    
            buffer.rewind();
    
            return buffer;
        }
    }
    

提交回复
热议问题