简体   繁体   English

用Java编写多个文件的最快方法

[英]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. 我有需要将多个输入流写入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. 我有多个文件,大小从45到400 mb不等,对于典型的45 mb和360 mb文件,此方法平均需要3分钟左右。 Can this be further improved? 可以进一步改善吗?

You could try a BufferedInputStream 您可以尝试BufferedInputStream

As @StephenC replied is it unrelevant in this case to use a BufferedInputStream because the buffer is big enough. 正如@StephenC回答的那样,在这种情况下使用BufferedInputStream是无关紧要的,因为缓冲区足够大。

I reproduced the behaviour on my computer (with an SSD drive). 我在计算机(带有SSD驱动器)上重现了该行为。 I took a 100MB file. 我拿了一个100MB的文件。

  • It took 110ms to create the new file with this example. 通过此示例创建新文件花了110毫秒。
  • With an InputStreamBuffer and an OutputStream = 120 ms. 使用InputStreamBuffer和OutputStream = 120 ms。
  • With an InputStream and an OutputStreamBuffer = 120 ms. 使用InputStream和OutputStreamBuffer = 120 ms。
  • With an InputStreamBuffer and an OutputStreamBuffer = 110 ms. 使用InputStreamBuffer和OutputStreamBuffer = 110 ms。

I don't have a so long execution time as your's. 我没有你那么长的执行时间。

Maybe the problem comes from your readerSuppliers ? 也许问题来自您的readerSuppliers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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