Java Code to monitor changes in a text file
I need to write a java code which detects changes done to a text file and also notify what change was done to the text file when the text file is changed ie) If a word ABCD was changed to BCDA in the text file then i need to be notified that ABCD was changed to BCDA. Can you please let m开发者_如何学编程e know whether i can achieve this using java(using only opensource and no paid or commercial tools)
See http://commons.apache.org/io/api-release/index.html?org/apache/commons/io/monitor/package-summary.html for an open-source library that will let you watch a file for changes.
EDIT: Note that this will report that a change occured, but not the specific line that changed. If you want a diff to occur in real time you'll probably have to run the diff yourself: I'm uncertain any prepackaged code exists that will do the diff for you.
For a more standard solution, you can also take a look at JSR 203 backport for Java 6. it provides File Scanner facility, compatible with upcoming Java runtime.
Sounds like you want to run a real-time diff.
Keep track of the "old" version of the file. Then have a piece of code that runs every 5 minutes or however fine-grained you need. Run a diff between these two files using either your own method or a library method (see java-diff-utils).
精彩评论