日期:2014-05-20 浏览次数:20950 次
/**
* 递归扫描整个路径
*
* @param file
* 要扫描的路径
* @param dirFileMap
* 存放扫描结果
*/
private void totalScan(File file, Map<String, String> dirFileMap) {
String[] fileList = file.list();
// 判断是否为空目录
if (null != fileList) {
for (int i = 0; i < fileList.length; i++) {
String pname = file.getAbsolutePath() + "\\" + fileList[i];
File tempFile = new File(pname);
if (tempFile.isDirectory()) {
dirFileMap
.put("文件夹:" + pname, tempFile.lastModified() + "");//修改了此处
totalScan(tempFile, dirFileMap);
} else {
// 不相同的文件夹下,存放的文件可能名字相同,但是同一路径下的文件肯定不会相同,
// 所以采用全路径做为key值
dirFileMap.put("文件:" + pname, tempFile.lastModified() + "");//修改了此处
}
}
}
}
/**
* 得到增加的文件及文件夹,并增加到已有的文件信息中
*/
private void getAddedFile(Map<String, String> nowDirFileMap) {
for (Iterator<String> iterator = nowDirFileMap.keySet().iterator(); iterator
.hasNext();) {
String key = iterator.next();
String oldValue = oldDirFileMap.get(key);
String newValue = nowDirFileMap.get(key);
if (null == oldValue) {
oldDirFileMap.put(key, newValue);
System.out.println("新增的" + key);
}
}
}
/**
* 得到删除的文件及文件夹,并删除已经不存在的文件信息
*/
private void getDeletedFile(Map<String, String> nowDirFileMap) {
for (Iterator<String> iterator = oldDirFileMap.keySet().iterator(); iterator
.hasNext();) {
String key = iterator.next();
String oldValue = oldDirFileMap.get(key);
String newValue = nowDirFileMap.get(key);
if (null == nowDirFileMap.get(key)) {
System.out.println("删除的" + key);
iterator.remove();
oldDirFileMap.remove(key);
} else if (!newValue.equals(oldValue)) {//修改了此处
System.out.println("修改的" + key);
oldDirFileMap.put(key, newValue);
}
}
}
/**
* 展示原有文件
*/
private void displayNowFile() {
System.out.println(dir + "路径原有文件目录如下:\n");
Set set = oldDirFileMap.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());//此处修改
}
System.out.println("========================");
}