Java 编写定时删除文件程序
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class FileDeleter extends TimerTask {
private static final String targetPath1 = “C:\\Program Files\\Tencent\\QQ\\…….\\image”;
private static final String targetPath2 = “C:\\Program Files\\Tencent\\QQ\\………..\\QQPhoto”;
private Timer timer = new Timer();
private Calendar calendar = Calendar.getInstance();
public static void main(String[] args) {
FileDeleter fm = new FileDeleter();
fm.start();
}
private static void delFiles(String[] dir) {
for (int i = 0; i < dir.length; i++) {
File f = new File(dir_);
File[] files = null;
if (f.exists()) {
files = f.listFiles();
for (int j = 0; j < files.length; j++) {
files[j].delete();
}
}
}
}
public void start() {
// 每隔 5 分钟删除一次
timer.scheduleAtFixedRate(this, new Date(), 1000 * 300);
}
public void run() {
delFiles(new String[] {targetPath1, targetPath2});
// 下班之前停止任务 ~.~
if(calendar.get(Calendar.HOUR_OF_DAY) == 17 && calendar.get(Calendar.MINUTE) == 15){
this.cancel();
}
}
}_