Sample method of java file monitoring watchservice

summary

java1. 7 provides a watchservice to monitor the changes of files in the system. The monitoring is a file system monitor based on the operating system, which can monitor the changes of all files in the system. This monitoring does not need traversal and comparison. It is a monitoring based on signal transceiver, so the efficiency must be the highest; Now that Java has wrapped it, you can use the OS file system monitor directly in Java programs.

Usage scenario

The above two scenarios are more suitable for file monitoring using watchservice.

Example

1. Use path to specify the directory to monitor

2、Path. The register () method registers those events (create, modify, delete) to monitor the specified directory

3. Call watchservice take(); Get the watchkey that monitors the changes of the directory file. This method is a blocking method. If there is no file modification, it will be blocked all the time.

4. Traverse all modification events and handle them accordingly.

5. Once monitoring is completed, the monitor needs to be reset.

Disadvantages of not using watchservice monitoring

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>