a- package test;
- 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 {
- /**
- * 定时删除指定目录
- * @throws ParseException
- */
- public static void main(String[] args) throws ParseException {
- // TODO Auto-generated method stub
- Timer t = new Timer();
- String s = "2015.9.11 17:04:00";//计划任务执行的时间
- SimpleDateFormat sp = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//对时间字符串进行解析
- Date d = sp.parse(s);
- System.out.println("任务将于"+d+"执行!!");
- t.schedule(new MyTask(t),d);
- }
- }
- //建立任务
- class MyTask extends TimerTask{
- private Timer t = null;
- //重写run()实现删除文件
- MyTask(Timer t){
- this.t = t;
- }
- public void run(){
- //封装目录
- File srcFolder = new File("C:\\Users\\hyning\\Desktop\\aaa");
- deleteFolder(srcFolder);
- System.out.println("Misson done!!");
- t.cancel();
- }
- public void deleteFolder(File srcFolder) {
- // TODO Auto-generated method stub
- File[] fileArr = srcFolder.listFiles();
- //遍历
- for(File f:fileArr){
- if(f.isDirectory()&&!(f.isHidden())){
- deleteFolder(f);
- f.delete();
- }
- else{
- f.delete();
- }
- }
- }
- }
复制代码
|
|