首页 > 代码库 > 用定时器在某个时间点删除文件夹

用定时器在某个时间点删除文件夹

package cn.idcast8;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

class DeleteFolder extends TimerTask {
	// 用定时器删除文件夹
	@Override
	public void run() {
		// 定义要删除的文件夹
		File srcFolder = new File("demo");
		// 写一个方法进行调用
		deleteFolder(srcFolder);
	}

	// 递归删除文件夹
	public void deleteFolder(File srcFolder) {
		File[] filearray = srcFolder.listFiles();
		if (filearray != null) {
			for (File file : filearray) {
				if (file.isDirectory()) {
					deleteFolder(file);
				} else {
					System.out.println(file.getName() + ":" + file.delete());
				}
			}
			System.out.println(srcFolder.getName() + ":" + srcFolder.delete());
		}
	}
}

public class TimerTest {
	public static void main(String[] args) throws ParseException {
		Timer t = new Timer();
		String s = "2017-03-05 13:14:00";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = sdf.parse(s);
		t.schedule(new DeleteFolder(), d);

	}
}

  

用定时器在某个时间点删除文件夹