Java编写定时删除文件程序
/*Java教程:http://www.javaweb.cc*/
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[i]);
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();
}
}
}