public static void main(String[] args) {
String path="D://My Documents/桌面/file";
String properity =".txt";
DeleteFile df=new DeleteFile(path, properity);
}
}
public class DeleteFile {
private String path;
private String properity;
public File file;
public DeleteFile(String path, String properity) {
super();
this.path = path;
this.properity = properity;
File file = new File(path);
Delete(file);
}
public void Delete(File file) {
if (file.isFile()) {
String apath = file.getAbsolutePath();
int length = apath.length();
String property = apath.substring(length - 4, length);
if (property.equals(properity)) {
file.delete();
}
} else {
File[] a = file.listFiles();
for (int i = 0; i < a.length; i++) {
Delete(a[i]);
}
}
}
}
本文写了如何在主函数里面传入文件的目录和对应需要删除的文件属性,在myfile里面文件,通过构造函数的方式来递归调用delete方法是一个不太完美的方法,应该在myfile类里面写一个方法来调用delete。。