實(shí)現(xiàn)功能:實(shí)時監(jiān)控指定目錄下文件的增加 / 更新 / 刪除 情況.
-
- package monitor.file;
-
-
-
-
-
-
- public class FileMonitor {
- public static void main(String[] args) {
- FileSchedulerTask task = new FileSchedulerTask("D:\\Temp");
- FileScheduler monitor = new FileScheduler();
- monitor.schedule(task, new TimeStep());
- }
- }
-
-
- package monitor.file;
-
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
-
-
-
-
-
-
- public class FileScheduler {
-
- private final Timer timer;
-
- public FileScheduler(){
- timer = new Timer();
- }
- public FileScheduler(boolean isDaemon){
-
- timer = new Timer(isDaemon);
- }
-
-
-
-
-
- public void schedule(Runnable task,TimeStep step){
- Date time = step.next();
- SchedulerTimerTask timeTask = new SchedulerTimerTask(task,step);
-
- timer.schedule(timeTask, time);
-
-
-
-
- }
-
-
-
-
-
- private void reSchedule(Runnable task,TimeStep step){
- Date time = step.next();
- SchedulerTimerTask timeTask = new SchedulerTimerTask(task,step);
-
- timer.schedule(timeTask, time);
- }
-
-
-
- public void cancle(){
- timer.cancel();
- }
-
-
-
-
-
- private class SchedulerTimerTask extends TimerTask{
- private Runnable task;
- private TimeStep step;
-
- public SchedulerTimerTask(Runnable task,TimeStep step){
- this.task = task;
- this.step = step;
- }
- @Override
- public void run() {
-
- task.run();
-
- reSchedule(task, step);
- }
- }
- }
-
-
- package monitor.file;
-
- import java.io.File;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
-
-
-
-
-
-
- public class FileSchedulerTask implements Runnable{
-
- private boolean firstRun = true;
-
- private String directory = "";
-
- private Map<String,Long> currentFiles = new HashMap<String,Long>();
-
- private Set<String> newFiles = new HashSet<String>();
-
-
-
-
- public FileSchedulerTask(){
-
- }
- public FileSchedulerTask(String directory){
- this.directory = directory;
- }
-
-
-
- public void run() {
- File file = new File(directory);
- if(firstRun){
- firstRun = false;
-
- loadFileInfo(file);
- System.out.println("----- init success -----");
- } else{
-
- checkFileUpdate(file);
-
- checkRemovedFiles();
-
- newFiles.clear();
- }
- }
-
-
-
-
- private void loadFileInfo(File file){
- if(file.isFile()){
- currentFiles.put(file.getAbsolutePath(), file.lastModified());
- return;
- }
- File[] files = file.listFiles();
- for(int i=0;i<files.length;i++){
- loadFileInfo(files[i]);
- }
- }
-
-
-
-
- private void checkFileUpdate(File file){
- if(file.isFile()){
-
- newFiles.add(file.getAbsolutePath());
-
- Long lastModified = currentFiles.get(file.getAbsolutePath());
- if(lastModified == null){
-
- currentFiles.put(file.getAbsolutePath(), file.lastModified());
- System.out.println("添加文件:" + file.getAbsolutePath());
- return;
- }
- if(lastModified.doubleValue() != file.lastModified()){
-
- currentFiles.put(file.getAbsolutePath(), file.lastModified());
- System.out.println("更新文件:" + file.getAbsolutePath());
- return;
- }
- return;
- } else if(file.isDirectory()){
- File[] files = file.listFiles();
- if(files == null || files.length == 0){
-
- return;
- }
- for(int i=0;i<files.length;i++){
- checkFileUpdate(files[i]);
- }
- }
- }
-
-
-
- private void checkRemovedFiles(){
-
-
-
- if(newFiles.size() == currentFiles.size()){
-
- return;
- }
- Iterator<String> it = currentFiles.keySet().iterator();
- while(it.hasNext()){
- String filename = it.next();
- if(!newFiles.contains(filename)){
-
-
-
- it.remove();
- System.out.println("刪除文件:" + filename);
- }
- }
- }
-
-
-
-
- public String getDirectory() {
- return directory;
- }
- public void setDirectory(String directory) {
- this.directory = directory;
- }
- }
-
-
- package monitor.file;
-
- import java.util.Calendar;
- import java.util.Date;
-
-
-
-
-
-
- public class TimeStep {
- private Calendar calendar = Calendar.getInstance();
-
- private int field = Calendar.SECOND;
-
- private int amount = 10;
-
-
-
-
-
- public int getField() {
- return field;
- }
- public void setField(int field) {
- this.field = field;
- }
-
-
-
-
- public int getAmount() {
- return amount;
- }
- public void setAmount(int amount) {
- this.amount = amount;
- }
-
-
-
-
-
- public Date next(){
- calendar.add(field, amount);
- return calendar.getTime();
- }
- }
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。