文件复制后目录大小不匹配
希望有人以前见过这个。我试图将所有目录内容从源复制到不同的目录,为此我开始使用 Commons FileUtils.copyDirectorytoDirectory 方法(File src,File dest)。代码非常简单:
public static void copyDirtoDir(String src, String dest) {
File s = new File(src);
File d = new File(dest);
try {
FileUtils.copyDirectoryToDirectory(s, d);
} catch (IOException e) {
e.printStackTrace();
}
}
要在 Linux 上运行此测试,我将应用程序作为 JAR 运行,并从命令行传递 src 和 dest 字符串。问题是,当我在执行后检查生成的目录大小时,大小存在巨大差异(复制的目录大约是原始目录大小的两倍 - 使用“du -sh”检查)。
然后我简单地尝试使用 nio.FileChannels,如下所示:
public static void copyFile(File in, File out) throws IOException {
FileChannel source = new FileInputStream(in).getChannel();
FileChannel destination = new FileOutputStream(out).getChannel();
source.transferTo(0, source.size(), destination);
source.close();
destination.close();
}
为目录中的每个文件调用此方法。这种变化产生的尺寸也大约是原始尺寸的两倍。如果我列出目录内容,它们是相同的。
是否缺少任何参数或可能导致此大小差异的原因?
Hopefully someone has seen this before. I'm trying to copy all directory contents from the source to a different directory, and for this I started using the Commons FileUtils.copyDirectorytoDirectory method(File src, File dest). The code is pretty simple:
public static void copyDirtoDir(String src, String dest) {
File s = new File(src);
File d = new File(dest);
try {
FileUtils.copyDirectoryToDirectory(s, d);
} catch (IOException e) {
e.printStackTrace();
}
}
To run this test on Linux, I'm running the app as a JAR and passing the src and dest strings from the command line. The problem is that when I check the resulting directory size after execution, there's a huge difference in size (with the copied dir around twice the size of the original - checked using 'du -sh').
I then simply tried with nio.FileChannels, as follows:
public static void copyFile(File in, File out) throws IOException {
FileChannel source = new FileInputStream(in).getChannel();
FileChannel destination = new FileOutputStream(out).getChannel();
source.transferTo(0, source.size(), destination);
source.close();
destination.close();
}
Calling this method for every file inside the directory. The resulting size from this variation is also around twice the size of the original. If I do a listing of the directories' contents, they are the same.
Is there any missing parameter or something that could be causing this size difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定发生了什么,但您可以 使用 diff比较目录。我相信这会很容易地确定差异。
Not sure what's going on, but you can use diff to diff directories. I'm sure that will pin down the differences easily.
javadoc 表示,
copyDirectoryToDirectory
将源目录及其所有内容复制到指定目标目录中的同名目录中。如果没有看到你的目录结构,我猜这可能会导致双数据。您不使用简单的 FileUtils.copyDirectory() ?
The javadoc says that
copyDirectoryToDirectory
copies the source directory and all its contents to a directory of the same name in the specified destination directory.Without seeing your directory structure, I'm guessing this may cause the double data. Any reason why you're not using the simple FileUtils.copyDirectory() ?