【发布时间】:2026-01-27 13:20:14
【问题描述】:
我想删除位于本地机器上的文件,与服务器机器相比。
我的例子:
import java.io.*;
public static void main(String[] args) throws Exception {
Set<String > lmd5 = new HashSet<String>();
lmd5.add("4be1babb2f8cac64d96f8052c0942130");
lmd5.add("a7514d56f233a434c7066176933d708d");
lmd5.add("d41d8cd98f00b204e9800998ecf8427e");
lmd5.add("674e3b94be9ed5db8bafe75808385de1");
Set<String > dmd5 = new HashSet<String>();
dmd5.add("4be1babb2f8cac64d96f8052c0942130");
dmd5.add("a7514d56f233a434c7066176933d708d");
dmd5.add("d41d8cd98f00b204e9800998ecf8427e");
if(lmd5.equals(dmd5)){
System.out.println("OK");
}
else{
lmd5.removeAll(dmd5);
System.out.println("Obsoletes Files To Delete : " + lmd5);
File[] paths = baseModDirectoryFile.listFiles();
for(File path:paths){
FileInputStream fis = new FileInputStream(path);
String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
if(lmd5.contains(md5) ){
File foundFile = path;
System.out.println("Obsolete File Found !");
try{
if(foundFile.delete()){
System.out.println("Obsolete File Deleted !");
}
else{
System.out.println("Obsolete File Not Deleted : Error !");
}
}catch(Exception e){
e.printStackTrace();
}
}
else{
continue;
}
}
}
}
在我的输出控制台中,出现“找到过时的文件”消息,但之后出现消息:“未删除过时的文件”。我相信我在删除文件的功能上来得太晚了,因为所有文件都已经检查过了。
也许我必须重新审视这个职位,但我想得到一些建议。
谢谢!
【问题讨论】:
标签: java file path md5 hashset