FileOutputStream.close is really slow when writing large file

后端 未结 2 1604
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 02:04

I have a method which receives a file over a TCP socket using this code:

FileOutputStream fileStream = new FileOutputStream(filename.getName());
while (total         


        
相关标签:
2条回答
  • 2020-12-04 02:47

    I saw the same with using filestream. What I found there was if you open the file as readwrite, it cached everything and didn't write until you close or dispose. Flush didn't write. However if your writes extend the size of the file it would autoflush.

    Opening as just write autoflushed on each write.

    0 讨论(0)
  • 2020-12-04 03:00

    Depending on the OS, flush() does nothing more thaqn force the data to be written to the OS. In the case of FileOutputStream, write() passes all data to the OS, so flush() does nothing. Where as close() can ensure the file is actually written to disk (or not depending on the OS). You can look at whether the disk is still busy when writing the data.

    A 500 MB files taking 30 seconds means you are writing 17 MB/s. This sounds like a very slow disk or that the file in on network share/drive.


    You can try this

    File file = File.createTempFile("deleteme", "dat"); // put your file here.
    FileOutputStream fos = new FileOutputStream(file);
    long start = System.nanoTime();
    byte[] bytes = new byte[32 * 1024];
    for (long l = 0; l < 500 * 1000 * 1000; l += bytes.length)
        fos.write(bytes);
    long mid = System.nanoTime();
    System.out.printf("Took %.3f seconds to write %,d bytes%n", (mid - start) / 1e9, file.length());
    fos.close();
    long end = System.nanoTime();
    System.out.printf("Took %.3f seconds to close%n", (end - mid) / 1e9);
    

    prints

    Took 0.116 seconds to write 500,006,912 bytes
    Took 0.002 seconds to close
    

    You can see from the speed that on this system its not writing the data even on a close. i.e. the drive is not that fast.

    0 讨论(0)
提交回复
热议问题