注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

java 7中的文件监控器(file monitor)  

2012-02-19 21:08:19|  分类: java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.demo.test;

import java.io.IOException;
import java.nio.file.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daniel
 */
public class FileMonitor implements Runnable {

    private Path path = null;
    private AtomicBoolean stop = null;

    public static void main(String[] args) {
        AtomicBoolean stop = new AtomicBoolean(false);
        Path myDir = Paths.get("D:/");
        Thread t = new Thread(new FileMonitor(myDir, stop));
        t.start();
    }

    public FileMonitor(Path path, AtomicBoolean stop) {
        this.path = path;
        this.stop = stop;
    }

    @Override
    public void run() {
        try {
            WatchService watcher = path.getFileSystem().newWatchService();
            path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE,
                    StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
            WatchKey watckKey = watcher.take();
            while (!stop.get()) {
                List<WatchEvent<?>> events = watckKey.pollEvents();
                for (WatchEvent event : events) {
                    if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                        System.out.println("Created: " + event.context().toString());
                    }
                    if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
                        System.out.println("Delete: " + event.context().toString());
                    }
                    if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
                        System.out.println("Modify: " + event.context().toString());
                    }
                }
                Thread.currentThread().sleep(500);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(FileMonitor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotDirectoryException ex) {
            System.err.println("Not a directory. Will exit.");
        } catch (IOException ex) {
            Logger.getLogger(FileMonitor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void stop() {
        stop.compareAndSet(false, true);
    }
}

启动该线程后,在D盘下增加、修改和删除文件(注意,是文件不是文件夹),那么就会触发事件。

  评论这张
 
阅读(1298)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017