1.制作一个任务:
1).定义类,继承自TimerTask
2).重写run;
递归删除目录
2.定义"定时器"
class MyTimerTask extends TimerTask{
private Timer timer;
public MyTimerTask(Timer t){
this.timer = t;
}
@Override
public void run() {
File file = new File("C:\\20150822 - 副本");
deleteFile(file);
System.out.println("删除完毕!");
this.timer.cancel();
}
private void deleteFile(File file) {
if(file == null){
return;
}
if(file.isFile()){//文件
//直接删除
file.delete();
}else{//目录
//获取目录下所有的文件和目录
File[] fileArray = file.listFiles();
for(File f : fileArray){
deleteFile(f);
}
//删除目录
file.delete();
}
}
}
public class Demo {
public static void main(String[] args) throws ParseException {
Timer timer = new Timer();
String str = "2015-09-26 15:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(str);
timer.schedule(new MyTimerTask(timer), date);
}
}
|
|