How to Compare CSharp, Java, Python, Ruby files
You are working in an IT project on one product. You need to compare two CS files and accept or reject the found changes found in the specified range. With this action, you will merge the two files into one with the edits from different developers.
Files | |
---|---|
Source | |
Target |
GroupDocs.Comparison provides the ability to compare two files in CS format(or any other supported file formats).
The following are the steps to compare two CS files
- Instantiate Comparer objectwith source document path or stream;
- Call add method and specify path target document path or stream;
- Call compare method;
- Call getChanges method and obtain detected changes list;
- Set ComparisonAction of needed change object to ComparisonAction.ACCEPT or ComparisonAction.REJECT value;
- Call applyChanges method and pass collection of changes to it.
ApplyChangeOptions class:
- getChanges - List of changes that must be applied (or not) to the resulting document;
The following code samples demonstrate how to compare two CS files and accept or reject detected changesin a specific range.
try (Comparer comparer = new Comparer(SOURCE_FILE)) {
comparer.add(TARGET_FILE); // NOTE: Put here actual path to target document
comparer.compare();
ChangeInfo[] changes = comparer.getChanges();
for (int n = 0; n < 5; n++) {
changes[n].setComparisonAction(ComparisonAction.REJECT);
}
for (int n = 6; n < 17; n++) {
changes[n].setComparisonAction(ComparisonAction.ACCEPT);
}
comparer.applyChanges(RESULT_FILE, new SaveOptions(), new ApplyChangeOptions(changes));
}
As a result, we get a merged CS file where the deleted elements are marked in red, the added – in blue, and the modified – in green.
Also, you will receive a file in HTML format with changed places in the code.
Result File |
---|