To implement this "file monitoring" feature, the program must be able to detect what is happening in the file system-related folders. One way is to check what happens to the file system. But such an approach is inefficient. It cannot monitor open files or folders on a large scale. Another way is to use the Java.nio.file package to provide a file monitoring service. This service agrees to monitor your service for a folder or multiple folders.
At the time of registration. Just need to tell the events that need to be monitored, including the creation, deletion, and alteration of files. When the service monitors the corresponding event, it runs the event of the registration. The event in the register will have a thread or pool of threads to monitor the events that have been registered, so that when a register event occurs, it will be processed accordingly.
file Folder monitoring service Watchservice is easy to use and you can even define it yourself. You can use it directly or extend it on top of the original to make it more suitable for your particular needs.
The basic process for creating a folder monitoring service is as follows:
1, create a file system monitoring service-watchservice.
2, in the file system monitoring service to register you need to monitor the file folder. When you register a folder. Specifies the type of monitoring.
3, write an infinite wait. An event method that monitors the occurrence of an event. In order to be able to respond and terminate immediately after the event that the folder in the queue is monitored for response.
4, the whole monitoring process is the key. Ability to get the monitored file name from the monitoring key provided by the monitoring service
5, monitoring to wait for the occurrence of the key is to be able to run the required process.
6. After the event is monitored, wait again.
7. When the monitoring service exits or shuts down, the method of internal shutdown is called to close.
......
in order to more clearly explain the entire file folder running process. I have found a better document for you, links such as the following:
http://blog.csdn.net/cxhzqhzq/article/details/6604300
Java Advanced Applications (one)-Folder monitoring service