我需要递归比较用户选择的两个文件夹(源和目标)的内容(包括所有子文件夹)。内容必须放入 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**/
}
}
}
最佳答案
Google 是你的 friend :).. 这是我发现与你需要的非常相似的东西...... http://www.java2s.com/Tutorial/Java/0180__File/Utilityclassforsynchronizingfilesdirectories.htm
关于java - 如何引用不同目录子结构中的文件/目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34061512/