方法如下:(返回0表示一致,返回-1表示不一致)
public int CheckFile(String fromFilePath, String toFilePath) {
MitechFilesInfo fromFile = new MitechFilesInfo(fromFilePath); // 获取路径1下文件的信息
MitechFilesInfo toFile = new MitechFilesInfo(toFilePath);// 获取路径2下文件的信息
if (fromFile.getCheck() == -1 || fromFile.getCheck() == -1) { // 如果其中一个路径不存在
return -1;
} else if (fromFile.getDirectoryAmounts() != toFile
.getDirectoryAmounts()) {// 如果两个路径下文件夹个数不一致
return -1;
} else if (fromFile.getFileAmounts() != toFile.getFileAmounts()) {// 如果两个路径下文件个数不一致
return -1;
} else { // 比较两个路径下文件的大小
List<File> fromFileList = fromFile.getFileList(); // 获取路径1下文件List
List<File> toFileList = toFile.getFileList(); // 获取路径2下文件List
if (fromFileList.size() == 0 || toFileList.size() == 0) { // 如果其中一个路径下文件List大小为0返回-1
return -1;
}
Iterator<File> fromFileIterator = fromFileList.iterator();
while (fromFileIterator.hasNext()) { // 遍历fromFile
File tempFromFile = (File) fromFileIterator.next();
String tempFromFileName = tempFromFile.getName();
long tempFromFileLength = tempFromFile.length();
boolean Same = false;
Iterator<File> toFileIterator = toFileList.iterator();
while (toFileIterator.hasNext()) { // 遍历toFile
File tempToFile = (File) toFileIterator.next();
if (tempToFile.getName().equals(tempFromFileName)) {
if (tempToFile.length() == tempFromFileLength) { // 长度一致
Same = true;
break;
}
break; // 找到但长度不一致
}
}
if (!Same) {
return -1;
}
}
return 0;
}
}