In this article, and following the Observer pattern, we are going to design a high-level API on top of this mechanism for listening to file events for a given folder.
We will begin by creating a FileEvent class, which extends the java. EventObject from which all event state objects shall be derived. A FileEvent instance is constructed with a reference to the source, which is logically the file upon which the event occurred upon. Next, we create the FileListener interface that must be implemented by an observer in order to be notified for file events.
It extends the java. EventListener interface, which is a tagging interface that all event listener interfaces must extend. The last piece of the puzzle is to create the subject, which maintains the list of observers, and notifies them of any state changes, by calling one of their methods.
We are going to name it FileWatcher and given a folder, this is how an instance of this class is constructed. It can implement the Runnable interface, so we can start the watch process with a daemon thread when invoking its watch method if the folder exists. In the implementation of its run method, a WatchService instance is created to poll for events within a try-with-resources statement.
We will keep a track of it using a static final list in the FileWatcher class, so we can later invoke its close method to cause any thread waiting to retrieve keys, to throw the unchecked ClosedWatchServiceException , which will interrupt the watch process in a clean way. Therefore, we will get no memory leak warnings when the application is being gracefully shutdown. Whenever an event occurs, the file path is resolved and the listeners are notified accordingly. If it is the creation of a new folder, another FileWatcher instance will be created for its monitoring.
Here is the complete listing of the FileWatcher class. The final touch of our design can be the creation of a FileAdapter class, which provides a default implementation of the FileListener interface so that we can process only few of the events to save code. Note that Apache VFS uses a polling algorithm, although it may offer greater functionality. Also note that the API does not offer a way to determine whether a file has been closed. Java commons-io has a FileAlterationObserver.
Similar to commons VFS. The advantag is that it has much less dependencies. But it uses java File instead of the VFS abstraction layer. I run this snippet of code every time I go to read the properties file, only actually reading the file if it has been modified since the last time I read it.
Hope this helps someone. Similar approach is used in Log4J FileWatchdog. Should be in JDK7. Update: Was added to Java SE 7. Chris Janicki offers a link to the relevant Java tutorial. If you are willing to part with some money, JNIWrapper is a useful library with a Winpack, you will be able to get file system events on certain files. Unfortunately windows only.
Otherwise, resorting to native code is not always a bad thing especially when the best on offer is a polling mechanism as against a native event. I've noticed that Java file system operations can be slow on some computers and can easily affect the application's performance if not handled well. Although this API seems to be focused on dynamic compilation of classes, it also includes classes in its API that monitors file changes.
Pretty sure it's cross platform I've used it on mac, linux, and windows. There is a commercial cross-desktop library for files and folders watching called JxFileWatcher. Similar to the other answers, here's how I did it using File, Timer, and TimerTask to let this run as a background thread polling at set intervals. Polling the last modified file property is a simple yet effective solution though.
Just define a class extending my FileChangedWatcher and implement the onModified method:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. File changed listener in Java Ask Question. Asked 12 years, 11 months ago.
Active 12 months ago. Viewed k times. Improve this question. Just a note. Brian Agnew Brian Agnew k 36 36 gold badges silver badges bronze badges. Stu Thompson Stu Thompson Why does it have to be Java? Yes, that is essentially what such a library would be, a wrapper.
But I was hoping that something like that already exists. Two more options that currently use polling: Apache Camel File Component Spring Integration File Watcher However, because both projects have large active communities, they will likely be updated in the future to support the Java 7 WatchService FileSystem.
Raman Raman A comment explaining the downvote would be helpful The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
Visit chat. Linked See more linked questions. Related
0コメント