如何引用不同目录子结构中的文件/目录?

时间:2015-12-03 08:53:19

标签: java recursion path directory

我需要递归地比较用户选择的两个文件夹(源和目标)的内容(包括所有子文件夹)。必须将内容放入TreeViews中,这将显示它们中的文件/目录如何使用某种颜色标记或其他类型相互比较。关系是:

Exists in source, but not in target -> new, copy over to target
Exists in target, but not in source -> deleted, delete from the target
Exists in both, but binary unequal -> changed, copy over from source
Exists in both, and is binary equal -> unchanged, leave be

然而,问题是以某种方式引用另一个目录,同时递归地通过其中一个目录。例如:

Source: C:\somewhere\fooSource
Target: C:\somewhereElse\barTarget

现在,对源文件夹的递归调用找到了C:\ somewhere \ fooSource \ aSubfolder。如何让Java将路径C:\ somewhereElse \ barTarget \ aSubfolder放在一起,以便程序可以检查是否存在或进行二进制比较(如果它是文件)?

编辑,实施的一些骨架:

@FXML
private void handleCompareButton() {
    if (sourceFile.exists() && targetFile.exists() && !sourceFile.equals(targetFile)) {
        for (File i : sourceFile.listFiles()) {
            if (i.isFile()) {
                if (/*the equivalent file in the other directory**/.exists()){
        /*do the checks for binary equality**/
                }
            }
/*some recursive calls to another function in case it's a directory**/
        }
    }
}

1 个答案:

答案 0 :(得分:1)

谷歌是你的朋友:) .. 继承人我发现的东西非常类似于你需要的东西...... http://www.java2s.com/Tutorial/Java/0180__File/Utilityclassforsynchronizingfilesdirectories.htm