复制代码即刻
package com.wenjy_25;
import java.io.File;
import java.util.Date;
import java.util.Timer;
public class Test {
public static void main(String[] args) throws Exception {
Timer t =new Timer();
//设置日期
String s = "2018-4-23 16:26:30";
//日期格式
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d =format.parse(s);
t.schedule(new DeleteFolder(), d);
}
}
class DeleteFolder extends java.util.TimerTask{
@Override
public void run() {
//文件路径
File file = new File("d://Demo//222");
delteFile(file);
}
//递归删除文件及文件夹
public void delteFile(File file){
File []filearray= file.listFiles();
if(filearray!=null){
for(File f:filearray){
if(f.isDirectory()){
delteFile(f);
}else{
f.delete();
}
}
file.delete();
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
}