Java 6 的 WatchService
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7968488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WatchService for Java 6
提问by paradigmatic
Java 7 introduced WatchService
for monitoring file systems continuously. Is there a backport for Java 6 ?
引入 Java 7WatchService
以持续监控文件系统。Java 6 是否有向后移植?
Are there pure Java libraries with similar features ?
是否有具有类似功能的纯 Java 库?
采纳答案by Thomas Uhrig
yes, of course. Apache VFS does exactly this. you can find it under http://commons.apache.org/vfs/. It's a pure java library that can monitor files and it's pretty easy to use:
当然是。Apache VFS 正是这样做的。您可以在http://commons.apache.org/vfs/下找到它。它是一个纯java库,可以监控文件,使用起来非常简单:
FileSystemManager manager = VFS.getManager();
FileObject file= manager.resolveFile("c:/MyFile.txt");
DefaultFileMonitor fm = new DefaultFileMonitor(new MyListener());
fm.setDelay(5000);
fm.addFile(file);
fm.start();
the code above will monitor the file c:/MyFile.txt. if it changes, the object new MyListener() is called.
上面的代码将监控文件 c:/MyFile.txt。如果它发生变化,则调用对象 new MyListener()。
回答by Jesse Glick
A pure Java library for this is impossible; you need a native component if you want to avoid polling.
一个纯粹的 Java 库是不可能的;如果您想避免轮询,您需要一个本机组件。
http://wiki.netbeans.org/NativeFileNotificationsgives some information about both the available native APIs and various Java libraries wrapping them.
http://wiki.netbeans.org/NativeFileNotifications提供了有关可用本机 API 和包装它们的各种 Java 库的一些信息。
回答by Boris Pavlovi?
Jetbrains IntelliJ IDEA has a component 'Virtual File System' which fires a native file system watcher notifying underlying file system changes back to IDEA. For two years Jetbrains has been releasing an open source version which should be containing the component. It works with Java 6.
Jetbrains IntelliJ IDEA 有一个组件“虚拟文件系统”,它触发一个本地文件系统观察器,通知底层文件系统更改回 IDEA。两年来,Jetbrains 一直在发布一个应该包含该组件的开源版本。它适用于 Java 6。
It's released under Apache 2.0 license as claimed in the FAQ page.
它是在 Apache 2.0 许可下发布的,如常见问题页面中所述。