java7新特性监听文件夹改变

Java 7 uses the underlying file system functionalities to watch the file system for changes. Now, we can watch for events like creation, deletion, modification, and get involved with our own actions. For accomplish this task, we need:

• An object implementing the Watchable interface - the Path class is perfect for this job.
• A set of events that we are interested in - we will use StandardWatchEventKind which implements the WatchEvent.Kind<T>.
• An event modifier that qualifies how a Watchable is registered with a WatchService.
• A watcher who watch some watchable – per example, a watcher that watches the File System for changes. The abstract class is java.nio.file.WatchService but we will be using the FileSystem object to create a watcher for the File System.

The below example follows the above scenario:

import java.nio.file.Path;  
import java.nio.file.Paths;  
import java.nio.file.StandardWatchEventKind;  
import java.nio.file.WatchEvent;  
import java.nio.file.WatchKey;  
import java.nio.file.WatchService;  
import java.util.List;  
public class Main {  
    public static void main(String[] args) {  
        //define a folder root  
        Path myDir = Paths.get("D:/data");         
        try {  
           WatchService watcher = myDir.getFileSystem().newWatchService();  
           myDir.register(watcher, StandardWatchEventKind.ENTRY_CREATE,   
           StandardWatchEventKind.ENTRY_DELETE, StandardWatchEventKind.ENTRY_MODIFY);  
           WatchKey watckKey = watcher.take();  
           List<WatchEvent<?>> events = watckKey.pollEvents();  
           for (WatchEvent event : events) {  
                if (event.kind() == StandardWatchEventKind.ENTRY_CREATE) {  
                    System.out.println("Created: " + event.context().toString());  
                }  
                if (event.kind() == StandardWatchEventKind.ENTRY_DELETE) {  
                    System.out.println("Delete: " + event.context().toString());  
                }  
                if (event.kind() == StandardWatchEventKind.ENTRY_MODIFY) {  
                    System.out.println("Modify: " + event.context().toString());  
                }  
            }  
             
        } catch (Exception e) {  
            System.out.println("Error: " + e.toString());  
        }  
    }  
} 

The FileSystem object and the WatchService can also be created like this:

  1. FileSystem fileSystem = FileSystems.getDefault();  
  2. WatchService watcher = fileSystem.newWatchService();  

And the Path (watchable), what we watch, and register it with the WatchService object like this:

  1. Path myDir = fileSystem.getPath("D:/data");  
  2. myDir.register(watcher, StandardWatchEventKind.ENTRY_CREATE,   
  3.           StandardWatchEventKind.ENTRY_DELETE, StandardWatchEventKind.ENTRY_MODIFY); 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值