尝试复制大文件时 NIO 出错 [英] Error with NIO while trying to copy large file

查看:51
本文介绍了尝试复制大文件时 NIO 出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将文件复制到另一个位置的代码.

I have the code to copy a file to another location.

public static void copyFile(String sourceDest, String newDest) throws IOException {

    File sourceFile = new File(sourceDest);
    File destFile = new File(newDest);
    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }

}
}

在复制小块(例如 300-400 Mb)时,一切都像魔术一样运作.但是当我试图复制一个 1.5 Gb 的文件时它失败了.堆栈是:

While copying small chunks, say, 300-400 Mb, everything works like magic. But when I tried to copy a file a size of 1.5 Gb it failed. The stack is:

运行:12.01.2011 11:16:36 FileCopier 主严重:复制文件时发生异常.再试一次.java.io.IOException:映射失败在 sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:748)在 sun.nio.ch.FileChannelImpl.transferFromFileChannel(FileChannelImpl.java:527)在 sun.nio.ch.FileChannelImpl.transferFrom(FileChannelImpl.java:590)在 FileCopier.copyFile(FileCopier.java:64)在 FileCopier.main(FileCopier.java:27)引起:java.lang.OutOfMemoryError: Map failed在 sun.nio.ch.FileChannelImpl.map0(本机方法)在 sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:745)... 4个构建成功(总时间:0 秒)

run: 12.01.2011 11:16:36 FileCopier main SEVERE: Exception occured while copying file. Try again. java.io.IOException: Map failed at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:748) at sun.nio.ch.FileChannelImpl.transferFromFileChannel(FileChannelImpl.java:527) at sun.nio.ch.FileChannelImpl.transferFrom(FileChannelImpl.java:590) at FileCopier.copyFile(FileCopier.java:64) at FileCopier.main(FileCopier.java:27) Caused by: java.lang.OutOfMemoryError: Map failed at sun.nio.ch.FileChannelImpl.map0(Native Method) at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:745) ... 4 more BUILD SUCCESSFUL (total time: 0 seconds)

我没有与 NIO 密切合作.你能帮我一下吗?在此先感谢您.

I haven't worked with NIO closely. Could you please help me out? Thank you so much in advance.

推荐答案

我想你可能已经被旧错误 前段时间已经遇到过.我不是要复制文件,而是要查找同样失败的内存映射文件.对我来说,解决方法是在循环中查找文件并请求 GC终结器 不时运行.

I think you might have been hit by an old bug which already encountered some time ago. I was not trying to copy a file but rather to seek through an memory-mapped file which failed as well. For me the workaround is to seek through the file in a loop and request the GC and finalizers to run every now and then.

内存映射的 ByteBuffers 在终结器中释放它们的映射并为新的映射腾出空间.这很丑陋,但至少它有效.让我们希望他们在即将到来的 NIO 迭代中对此有所作为.

The memory-mapped ByteBuffers release their mapping in the finalizer and make room for new mappings. This is very ugly, but at least it works. Let's hope they did something about this in the coming NIO iteration.

这篇关于尝试复制大文件时 NIO 出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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