【问题标题】:How copy files bigger than 4.3 GB in java如何在java中复制大于4.3 GB的文件
【发布时间】:2019-02-16 06:44:26
【问题描述】:

我正在编写一个程序部分,只是为了将文件从源文件复制到目标文件。该代码应该可以正常工作,但是如果有一个文件大文件,则在目标文件达到 4.3 GB 大小后,复制过程将结束,但有一个例外。例外是“文件太大”,它看起来像:

java.io.IOException: Die Datei ist zu groß
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.FileDispatcherImpl.write(FileDispatcherImpl.java:60)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:211)
at java.nio.channels.Channels.writeFullyImpl(Channels.java:78)
at java.nio.channels.Channels.writeFully(Channels.java:101)
at java.nio.channels.Channels.access$000(Channels.java:61)
at java.nio.channels.Channels$1.write(Channels.java:174)
at java.nio.file.Files.copy(Files.java:2909)
at java.nio.file.Files.copy(Files.java:3069)
at sample.Controller.copyStream(Controller.java:318)

制作方法如下:

    private void copyStream(File src, File dest){
    try {

        FileInputStream fis = new FileInputStream(src);
        OutputStream newFos = java.nio.file.Files.newOutputStream(dest.toPath(),StandardOpenOption.WRITE);

        Files.copy(src.toPath(),newFos);

        newFos.flush();
        newFos.close();
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我也尝试使用 java.io Fileoutputstream 并以千字节的方式写入,但发生了相同的情况。如何复制或创建大于 4.3 GB 的文件?是否可以使用除 java 之外的其他语言?这个程序我在 Linux (Ubuntu LTS 16.04) 上运行。

提前致谢。


编辑:

非常感谢大家的帮助。正如您所说,文件系统是问题所在。在我将文件系统格式化为 exfat 后,它工作正常。

【问题讨论】:

标签: java linux ubuntu-16.04 java-io


【解决方案1】:

POSIX(以及 Unix)系统允许对路径(从 File.getPath() 或路径的组件(您可以通过 File.getName() 获取的最后一个)获得的内容)施加最大长度。您可能由于文件的名称很长,因此会出现此问题。

在这种情况下,文件open 操作系统调用将失败并返回ENAMETOOLONG error code

但是,“文件太大”消息通常与“EFBIG”错误代码相关联。这更有可能是由write 系统调用引起的:

尝试写入的文件超过了与实现相关的最大文件大小或进程的文件大小限制。

也许正在打开文件以进行追加,并且文件末尾隐含的lseek 给出了EFBIG 错误。

最后,如果它必须对您的 RAM 进行某些操作,您可以尝试其他复制方法。

另一种选择可能是磁盘已满。

复制文件基本上有四种方法[事实证明流在基本层面上是最快的]:

使用流复制:

private static void copyFileUsingStream(File source, File dest) throws IOException {
    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(source);
        os = new FileOutputStream(dest);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
    } finally {
      is.close();
      os.close();
    }
}

使用 Java NIO 类复制:

private static void copyFileUsingChannel(File source, File dest) throws IOException {
    FileChannel sourceChannel = null;
    FileChannel destChannel = null;
    try {
        sourceChannel = new FileInputStream(source).getChannel();
        destChannel = new FileOutputStream(dest).getChannel();
        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
    }finally{
           sourceChannel.close();
           destChannel.close();
    }
}

使用 Apache Commons IO FileUtils 复制:

private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
    FileUtils.copyFile(source, dest);
}

以及使用 Java 7 和 Files 类的方法:

private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
    Files.copy(source.toPath(), dest.toPath());
}

编辑 1: 正如 cmets 中所建议的,这里有三个 SO 问题,它们涵盖了问题并更好地解释了四种不同的复制方法:

感谢@jww 指出

【讨论】:

  • 好的,谢谢。文件名和路径长度不是我尝试过的问题,它只是文件大小。我将尝试其他文件系统。
猜你喜欢
  • 2012-05-08
  • 2011-09-27
  • 2013-08-15
  • 2014-12-06
  • 2019-05-17
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多