import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DeleteFileByDate {
//这里指定文件保存的位置
private final String savePlace="d:/a/";
/**
* 查询并删除所有今天生成的文件
* @return
*/
public int DeleteFileDate(){
int number=0;
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String date=sdf.format(new Date());
File file=new File(savePlace);
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
String path=savePlace+tempList[i];
temp = new File(path);
if(temp.getName().startsWith(date)){//如果存在这个文件
System.out.println(temp.getName());
temp.delete();
number++;
}
}
return number;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DeleteFileByDate de=new DeleteFileByDate();
int flag=de.DeleteFileDate();
System.out.println("删除掉的文件总数为:"+flag);
}
}
原文:https://blog.csdn.net/bestcxx/article/details/50298637