Java 6 的 WatchService

发布于 2024-12-13 08:50:00 字数 1539 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静待花开 2024-12-20 08:50:00

是的当然。 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();

上面的代码将监视文件c:/MyFile.txt。如果发生变化,则调用对象 new MyListener()。

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:

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.

皓月长歌 2024-12-20 08:50:00

一个纯粹的 Java 库来做这件事是不可能的;如果你想避免轮询,你需要一个本机组件。

http://wiki.netbeans.org/NativeFileNotifications 提供了有关可用本机 API 和各种 Java 的一些信息包装它们的库。

A pure Java library for this is impossible; you need a native component if you want to avoid polling.

http://wiki.netbeans.org/NativeFileNotifications gives some information about both the available native APIs and various Java libraries wrapping them.

往事随风而去 2024-12-20 08:50:00

Jetbrains IntelliJ IDEA 有一个组件“虚拟文件系统”它会触发本机文件系统观察程序,将底层文件系统更改通知回 IDEA。两年来,Jetbrains 一直在发布包含该组件的开源版本。它适用于 Java 6。

它是在 Apache 2.0 许可证下发布的,如常见问题解答页面中所述。

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.

It's released under Apache 2.0 license as claimed in the FAQ page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文