Java实现文件监控之一
2016-09-08 09:12
447 查看
Java实现文件监控是一个比较常见的文件监控工具,但是JDK7提供的NIO2.0跟没有办法实现我们的的需求,局限性很大,所以一般都是自己DIY。但是在第三方插件也提供了相应的方法,健在下一篇中提及。那么闲言少叙,上代码:
1、设置常用变量
package watchFileTest; import java.util.Calendar; import java.util.Date; /** * Title: 文件监控之时间之实体类设置 * Description: TestDemo * @author: xg.chen * @date:2016年9月7日 */ public class TimeStep { //获取时间 private Calendar calendar = Calendar.getInstance(); //设置单位 private int feild = Calendar.SECOND; //最大数 private int amount = 10; public int getFeild() { return feild; } public void setFeild(int feild) { this.feild = feild; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } /** * MethodsTitle: 获取时间 * @author: xg.chen * @date:2016年9月7日 * @return */ public Date next(){ calendar.add(feild, amount); return calendar.getTime(); } }
2、设置文件定时器
package watchFileTest; import java.util.Date; import java.util.Timer; import java.util.TimerTask; /** * Title: 文件监控之定时器 * Description: TestDemo * @author: xg.chen * @date:2016年9月7日 */ public class FileScheduler { //定时器设置 private final Timer timer; //构造方法 public FileScheduler(){ timer = new Timer();; } //设置守护线程 public FileScheduler(boolean isDaemon){ // 是否为守护线程 timer = new Timer(isDaemon); } /** * MethodsTitle: 为定时器分配可执行任务 * @author: xg.chen * @date:2016年9月7日 * @param task * @param step */ public void schedule(Runnable task,TimeStep step){ Date time = step.next(); SchedulerTimerTask timeTask = new SchedulerTimerTask(task,step); // 安排在指定的时间 time 执行指定的任务 timetask timer.schedule(timeTask, time); } /** * MethodsTitle: 重新执行任务 * @author: xg.chen * @date:2016年9月7日 * @param task * @param step */ private void reSchedule(Runnable task,TimeStep step){ Date time = step.next(); SchedulerTimerTask timeTask = new SchedulerTimerTask(task,step); // 安排在指定的时间 time 执行指定的任务 timetask timer.schedule(timeTask, time); } /** * MethodsTitle: 停止当前定时器 * @author: xg.chen * @date:2016年9月7日 */ public void cancle(){ timer.cancel(); } /** * Title:停止当前定时器 * Description: TestDemo * @author: xg.chen * @date:2016年9月7日 */ private class SchedulerTimerTask extends TimerTask{ private Runnable task; private TimeStep step; public SchedulerTimerTask(Runnable task,TimeStep step){ this.task = task; this.step = step; } public void run() { // 执行指定任务 task.run(); // 继续重复执行任务 reSchedule(task, step); } } }
3、设置定时器任务
package watchFileTest; import java.io.File; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * Title: 文件监控之定时任务 * Description: TestDemo * @author: xg.chen * @date:2016年9月7日 */ 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>(); //文件起始目录之get/set方法 public String getDirectory() { return directory; } public void setDirectory(String directory) { this.directory = directory; } /** * 构造函数 */ public FileSchedulerTask(){ } public FileSchedulerTask(String directory){ this.directory = directory; } /** * 在 run() 中执行具体任务 */ public void run() { File file = new File(directory); if(firstRun){ firstRun = false; // 初次运行 loadFileInfo(file); System.out.println("-----init file successful-----"); } else{ // 检查文件更新状态[add,update] checkFileUpdate(file); // 检查被移除的文件[remove] checkRemovedFiles(); // 清空临时文件集合 newFiles.clear(); } } /** * MethodsTitle: 加载文件 * @author: xg.chen * @date:2016年9月7日 * @param file */ 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]); } } /** * MethodsTitle: 检查文件更新状态 * @author: xg.chen * @date:2016年9月7日 * @param file */ private void checkFileUpdate(File file) { if(file.isFile()){ // 将当前文件加入到 newFiles 集合中 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]); } } } /** * MethodsTitle: 检查被移除的文件 * @author: xg.chen * @date:2016年9月7日 */ 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)){ // 此处不能使用 currentFiles.remove(filename);从 map 中移除元素, // 否则会引发同步问题. // 正确的做法是使用 it.remove();来安全地移除元素. it.remove(); System.out.println("删除文件:" + filename); } } } }
4、测试
package watchFileTest; /** * Title: 测试类 * Description: TestDemo * @author: xg.chen * @date:2016年9月7日 */ public class TestMainClass { public static void main(String[] args) { FileSchedulerTask task = new FileSchedulerTask("D:\\tmp"); FileScheduler fileScheduler = new FileScheduler(); fileScheduler.schedule(task, new TimeStep()); } }
相关文章推荐
- Java技巧:java实现文件监控技巧分享
- java 实现文件监控
- java实现文件监控
- java实现文件监控
- java实现文件监控
- java 实现监控某目录文件变化
- Java 实现对文件系统的监控
- java实现文件实时监控:Jnotify和FileAlteration
- Java实现监控目录下文件变化
- java实现文件变化监控
- Java实现文件变化监控
- java实现文件变化监控的方法(推荐)
- Java实现文件监控之二
- java实现远程连接服务器,监控某个目录下的日志文件
- java 使用线程监控文件目录变化的实现方法
- java实现文件变化监控
- java jsch实现sftp文件上传,并且控制上传的速度,同时监控上传进度
- java 使用线程监控文件目录变化的实现方法
- JSch - Java实现的SFTP(文件上传详解篇)
- 使用Java实现多个文件压缩打包