import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; /*
* :在指定的时间删除指定目录
* 设置为一个计时任务
*/
public class TimerTest {
public static void main(String[] args) throws Exception {
Timer t = new Timer(); String s = "2016-04-09 20:46:59";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(s); t.schedule(new DeleteFolder(), d);
}
} class DeleteFolder extends TimerTask {
@Override
public void run() {
File srcFolder = new File("demo");
deleteFolder(srcFolder);
} private void deleteFolder(File srcFolder) {
File[] fileArr = srcFolder.listFiles();
if (fileArr != null) {
for (File file : fileArr) {
if (file.isDirectory()) {
deleteFolder(file);
} else {
file.delete();
}
}
}
// 该语句用于当文件夹已经为空时,直接将文件夹删除。
43 srcFolder.delete();
}
}