gpt4 book ai didi

java - 如何恢复 Mac 中使用 java 中的 File.delete() 方法删除的文件

转载 作者:行者123 更新时间:2023-12-02 01:15:05 26 4
gpt4 key购买 nike

我正在我的 Mac 上编写一个 Java 小工具来删除重复的文件,例如 file[1].jpg 它确实有效。之后我想快速修改它,以便像 file(1).png 这样的文件也被删除。我在我的正则表达式中犯了一个小错误,并犯了第二个错误,相信自己并认为我不需要测试它。现在我的桌面已经没有文件了。

java 是否只删除指向文件的指针,或者是否将文件移动到某处以便我可以恢复已删除的文件?它们不在垃圾箱中。有什么方法可以恢复这些文件吗?

import java.util.regex.*;
import java.util.List;
import java.util.ArrayList;

public class DuplicateFilesDelter{
public static void main(String [] args){
if(args.length!=0){
final File folder = new File(args[0]);
List<File> filesToDelete = new ArrayList<File>();
if(folder.isDirectory()&& folder.exists()){
try{
File[] files = folder.listFiles();
int duplicates = 0;

for(File file : files){
String name = file.getName();
if(Pattern.matches(".*\\[[1-9]\\].*",name)|| Pattern.matches(".*([1-9]).*",name)){
filesToDelete.add(file);
duplicates++;
}
}
System.out.println("Deleting " + duplicates + " duplicates from " + files.length);
int deleted = 0;
for(File f : filesToDelete){
try{
System.out.println("Delting " + f.getName() );
f.delete();
deleted++;
}catch(Exception e){}
}
files = folder.listFiles();
System.out.println("Deleted " + deleted + " Files from " + filesToDelete.size());
System.out.println(files.length + " Files left");

}catch(Exception e){
System.err.println(e.getMessage());
}
}else{
System.out.println( args[0] + "is Directory : " + folder.isDirectory());
System.out.println( args[0] + "exists : " + folder.exists());
}
}
}
}

最佳答案

Does Java delete just the pointer to the Files or does it move the files somewhere so that I can recover the deleted files?

Java 告诉操作系统通过进行 unlink 系统调用来删除 MacOS/Linux/UNIX 上的文件。

They are not in the Trash. Is there some way to recover these files?

不在 Java 中。您也许可以使用文件恢复工具来恢复它们。

关于java - 如何恢复 Mac 中使用 java 中的 File.delete() 方法删除的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58779959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com