Java Diff Utils is designed for that purpose.
final List<String> originalFileContents = new ArrayList<String>();
final String filePath = "C:/Users/BackSlash/Desktop/asd.txt";
FileListener fileListener = new FileListener() {
@Override
public void fileDeleted(FileChangeEvent paramFileChangeEvent)
throws Exception {
// use this to handle file deletion event
}
@Override
public void fileCreated(FileChangeEvent paramFileChangeEvent)
throws Exception {
// use this to handle file creation event
}
@Override
public void fileChanged(FileChangeEvent paramFileChangeEvent)
throws Exception {
System.out.println("File Changed");
//get new contents
List<String> newFileContents = new ArrayList<String> ();
getFileContents(filePath, newFileContents);
//get the diff between the two files
Patch patch = DiffUtils.diff(originalFileContents, newFileContents);
//get single changes in a list
List<Delta> deltas = patch.getDeltas();
//print the changes
for (Delta delta : deltas) {
System.out.println(delta);
}
}
};
DefaultFileMonitor monitor = new DefaultFileMonitor(fileListener);
try {
FileObject fileObject = VFS.getManager().resolveFile(filePath);
getFileContents(filePath, originalFileContents);
monitor.addFile(fileObject);
monitor.start();
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (FileNotFoundException e) {
//handle
e.printStackTrace();
} catch (IOException e) {
//handle
e.printStackTrace();
}
Where getFileContents
is :
void getFileContents(String path, List<String> contents) throws FileNotFoundException, IOException {
contents.clear();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
contents.add(line);
}
}
What I did:
- I loaded the original file contents in a
List<String>
.
- I used Apache Commons VFS to listen for file changes, using
FileMonitor
. You may ask, why? Because WatchService
is only available starting from Java 7, while FileMonitor
works with at least Java 5 (personal preference, if you prefer WatchService
you can use it). note: Apache Commons VFS depends on Apache Commons Logging, you'll have to add both to your build path in order to make it work.
- I created a
FileListener
, then I implemented the fileChanged
method.
- That method load new contents form the file, and uses
Patch.diff
to retrieve all differences, then prints them
- I created a
DefaultFileMonitor
, which basically listens for changes to a file, and I added my file to it.
- I started the monitor.
After the monitor is started, it will begin listening for file changes.
myFile.txt
. When the program is launched, it will retrieve the contents ofmyFile.txt
and store them in aList<String>
, let's call itoriginalFileContents
. Every timemyFile.txt
gets modified, we load the file into another list,newFileContents
. At this point we can dodiff(originalFileContents, newFileContents)
to find differences between the original file contents and the new ones. – Luckily