开发者

Directory size mismatch after file copy

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.copyDirec开发者_StackOverflowtorytoDirectory 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?


Not sure what's going on, but you can use diff to diff directories. I'm sure that will pin down the differences easily.


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() ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜