Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active January 9, 2023 13:44
Show Gist options
  • Save conholdate-gists/577571770609b0946531edf6fe1b9109 to your computer and use it in GitHub Desktop.
Save conholdate-gists/577571770609b0946531edf6fe1b9109 to your computer and use it in GitHub Desktop.
Compare and Merge Source Code Files in Java
//Instantiate Comparer object with source document path or stream;
try (Comparer comparer = new Comparer(SourceFile)) {
// Invoke add method and specify target document path or stream.
comparer.add(TargetFile);
//Invoke compare method.
final Path resultPath = comparer.compare(ResultPath);
}
//Initialize Comparer object with source file path
try (Comparer comparer = new Comparer(sourcePath)) {
//Invoke [add][] method and specify target file path.
comparer.add(targetPath);
//Call compare method
final Path resultPath = comparer.compare(outputPath);
//Get the list of changes by using getChanges method
ChangeInfo[] changes = comparer.getChanges();
//Set ComparisonAction of needed change object to ComparisonAction.ACCEPT value.
for (int i = 0; i < 10; i++) {
changes[i].setComparisonAction(ComparisonAction.ACCEPT);
}
//Set ComparisonAction of needed change object to ComparisonAction.REJECT value.
for (int i = 10; i < changes.length; i++) {
changes[i].setComparisonAction(ComparisonAction.REJECT);
}
// Call applyChanges method and pass collection of changes to it.
comparer.applyChanges(resultPath, new ApplyChangeOptions(changes));
}
//Initialize Comparer object with source file path
try (Comparer comparer = new Comparer(sourceFile)) {
//Invoke [add][] method and specify target file path.
comparer.add(targetFile);
//Call compare method
final Path resultPath = comparer.compare();
//Get the list of changes by using getChanges method
ChangeInfo[] changes = comparer.getChanges();
//Display the changes count
System.out.println("Count of changes: " + changes.length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment