简体   繁体   中英

Fastest way to write multiple files in java

I have requirement where I need to write multiple input streams to a temp file in java. I have the below code snippet for the logic. Is there a better way to do this in an efficient manner?

final String tempZipFileName = "log" + "_" + System.currentTimeMillis();
        File tempFile = File.createTempFile(tempZipFileName, "zip");
        final FileOutputStream oswriter = new FileOutputStream(tempFile);
        for (final InputStream inputStream : readerSuppliers) {
            byte[] buffer = new byte[102400];
            int bytesRead = 0;

            while ((bytesRead = inputStream.read(buffer)) > 0) {
                oswriter.write(buffer, 0, bytesRead);
            }
            buffer = null;
            oswriter.write(System.getProperty("line.separator").getBytes());
            inputStream.close();
        }

I have multiple files of size ranging from 45 to 400 mb, for a typical 45mb and 360 mb files this method is taking around 3 mins on average. Can this be further improved?

You could try a BufferedInputStream

As @StephenC replied is it unrelevant in this case to use a BufferedInputStream because the buffer is big enough.

I reproduced the behaviour on my computer (with an SSD drive). I took a 100MB file.

  • It took 110ms to create the new file with this example.
  • With an InputStreamBuffer and an OutputStream = 120 ms.
  • With an InputStream and an OutputStreamBuffer = 120 ms.
  • With an InputStreamBuffer and an OutputStreamBuffer = 110 ms.

I don't have a so long execution time as your's.

Maybe the problem comes from your readerSuppliers ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM