
Implement corresponding business logic processing in different methods as needed. Add a file system watcher to the monitor, and add a file listener.A custom file monitoring class that creates an observer FileAlterationObserver by specifying a directory.Customize the file listener class and inherit FileAlterationListenerAdaptor to handle the creation, modification, and deletion of files and directories.In this example, we will watch for all events made to a directory and print out any.
JAVA FILE MONITOR EXAMPLE HOW TO
The implementation of file monitoring commons-io is located in the .monitor package. Heres a simple example of how to use the Directory.getEvents() method. Next, we register a Path instance for the folder to be monitored with the types of.

Note that different versions require different JDK support, 2.7 requires Java 8 and above. Basic example of how to implement the monitor synchronization construct to allow mutual exclusion in threads - java-monitor-example/README. The first step is to create a new WatchService by using the newWatchService () method of the FileSystem class. Introduce the corresponding dependencies: commons-io commons-io 2.7 Here we need to use an open source framework to achieve this, which is the commons-io class library that almost every project will introduce. We use since we do not have any body in the request. We can choose to receive events when files or directories. var request HttpRequest.newBuilder (URI.create ('. If you encounter similar problems, you can search and solve them yourself. The Java WatchService can monitor a Path for changes to any file or directory in the hierarchy. I have not verified other operating systems. In Java 7, has been added, which enables monitoring of file changes.
JAVA FILE MONITOR EXAMPLE MAC
The API has also been raised on Stack Overflow about Java 7 having a delay under Mac OS, and even Windows and Linux systems. For example, the operation is frequent, and the efficiency is lost in traversing, saving the state, and comparing the state, and the functions of the OS cannot be fully utilized. Only files and directories in the current directory can be monitored, and subdirectories cannot be monitored.Īnd we also see that monitoring can only be regarded as quasi-real-time, and the monitoring time can only take the three values provided by the API by default. In the previous article Digging Through a Bug in the JDK Monitoring File, a specific example has been written, and its shortcomings have also been proposed.Ĭompared with Scheme 1, it is simple to implement and has high efficiency.

If there is a change, it means the file has been modified and needs to be reloaded or processed with corresponding business logic. Through timed tasks, the last modification time of the query file is polled and compared with the last time.

This solution is the simplest and most straightforward solution that comes to mind. Today I will share three solutions: Scheme 1: Scheduled task + File lastModified Of course, in other business scenarios, such as dynamic loading of configuration files, monitoring of log files, and monitoring of FTP file changes, similar scenarios will be encountered. When studying the rule engine, if the rules are stored in the form of files, it is necessary to monitor the specified directory or file to sense whether the rules have changed, and then load them.
