编写大文件时,FileOutputStream.close非常慢 [英] FileOutputStream.close is really slow when writing large file

查看:429
本文介绍了编写大文件时,FileOutputStream.close非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下代码通过TCP套接字接收文件的方法:

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

FileOutputStream fileStream = new FileOutputStream(filename.getName());
while (totalRead < size) {
    if (size - totalRead > CHUNKSIZE) {
        read = getInputStream().read(buffer, 0, CHUNKSIZE);
    } else {
        read = getInputStream().read(buffer, 0, size - totalRead);
    }
    totalRead += read;
    fileStream.write(buffer, 0, read);
    fileStream.flush();

    if (System.currentTimeMillis() > nextPrint) {
        nextPrint += 1000;
        int speed = (int) (totalRead / (System.currentTimeMillis() - startTime));
        double procent = ((double)totalRead / size) * 100;
        gui.setStatus("Reciving: " + filename + " at " + speed + " kb/s, " + procent + "% complete");
    }
}
gui.setStatus("Reciving: " + filename + " complete.");
fileStream.close();

FileOutputStream.close接收大文件需要很长时间,为什么会这样?如你所见,我在每个收到的块上刷新流..

FileOutputStream.close is taking really long time when receiving large files, why is that? As you see I'm flushing the stream at every received chunk..

推荐答案

取决于操作系统, flush()不再强制将数据写入操作系统。在FileOutputStream的情况下,write()将所有数据传递给OS,因此flush()不执行任何操作。其中 close()可以确保文件实际写入磁盘(或不依赖于操作系统)。您可以在写入数据时查看磁盘是否仍处于忙碌状态。

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.

一个500 MB的文件需要30秒才意味着你写的是17 MB / s。这听起来像一个非常慢的磁盘或网络共享/驱动器中的文件。

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.

你可以试试这个

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);

打印

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.

这篇关于编写大文件时,FileOutputStream.close非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆