Seb*_*n T 6 java linux java-io ubuntu-16.04
我正在编写程序部分只是为了将文件从源文件复制到目标文件.代码也可以正常运行,但是如果存在文件大文件,则在目标文件达到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)
Run Code Online (Sandbox Code Playgroud)
产生的方法如下:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用java.io Fileoutputstream并以kbyte方式写入,但发生的情况相同.如何复制或创建大于4.3 GB的文件?是否可能用java以外的其他语言?这个程序我运行在Linux(Ubuntu LTS 16.04)上.
提前致谢.
编辑:
非常感谢大家的帮助.正如你所说,文件系统是问题所在.在我将文件系统格式化为exfat后,它工作正常.
允许POSIX(以及Unix)系统在路径上强加一个最大长度(你得到的东西File.getPath()或路径的组成部分(你可以得到的最后一个File.getName()).你可能会看到这个问题因为长名为文件.
在这种情况下,文件open操作系统调用将失败并显示ENAMETOOLONG 错误代码.
但是,消息"文件太大"通常与'EBBIG'错误代码相关联.这更有可能是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();
}
}
Run Code Online (Sandbox Code Playgroud)
使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
使用Apache Commons IO FileUtils进行复制:
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
FileUtils.copyFile(source, dest);
}
Run Code Online (Sandbox Code Playgroud)
和使用Java 7和Files类的方法:
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
Run Code Online (Sandbox Code Playgroud)
编辑1:正如评论中所建议的,这里有三个SO问题,它们涵盖了问题并解释了四种不同的复制方法:
感谢@jww指出它
| 归档时间: |
|
| 查看次数: |
398 次 |
| 最近记录: |