私の Java プログラムは、テキスト ファイルの内容が変更されたかどうかを知る必要があります。現在、File.lastModified()
メソッドを使用してそれを実行していますが、理想的にはチェックを実行したくありません。代わりに、ファイルが変更されるたびにイベントが発生するようにします。この種のものに利用できるサードパーティのライブラリはありますか? を使用して達成できると聞きましたがapache file descriptors
、それに関する情報は見つかりませんでした。
4 に答える
There are many factors that might determine your solution. How often the files updates, what type the information is, etc...
My advice would be either the Java 7 standard, or the Apache de facto standard (if the requirement wont allow Java 7 solution)...
Apache configuration
If it is a file that is kind of property information, a configuration, then you might want to look at Apache commons configuration. Which gives a refresh method each time the file is updated, to reload your configuration, etc. http://commons.apache.org/proper/commons-configuration/userguide/howto_events.html#An_example
Java 7, WatchService
If you use Java 7, look at the WatchService, link http://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html
Apache IO
If you dont want to use Java 7, and the file is not a configuration (which is better to use commons configuration that way), then you might want to look at FileAlterationObserver from Apache io. http://commons.apache.org/proper/commons-io/
これは、新しいJava 7 WatchServiceで実現できます。これにより、ディレクトリを監視し、作成、変更、および削除イベントの通知を受けることができます。
Apache のDefaultFileMonitorを試してください。
問題のファイルを監視する必要があるファイルのリストに追加し、監視スレッドを開始するだけです。
ファイルが変更または削除された場合、OnChange イベントが発生します。
Google によると、 JNotifyやinotify-javaなどのライブラリを使用できます