How do I compare two ods documents programmatically in Java?
-
27-10-2019 - |
Question
I'm generating an ODS spreadsheet as an output from a Java program. I am currently trying to set up test cases for the same. In order to this, I need to compare the expected and actual outputs. I am currently using ODFToolkit to create the document.
How do I compare the two spreadsheets (expected and actual) in a Java program?
Solution
In case anyone needs the solution, here it is
public static boolean contentsAreIdentical(OdfSpreadsheetDocument document1, OdfSpreadsheetDocument document2) {
try {
ByteArrayInputStream bis1 = (ByteArrayInputStream) document1.getContentStream();
ByteArrayInputStream bis2 = (ByteArrayInputStream) document2.getContentStream();
if(bis1.available() != bis2.available()) {
return false;
}
while(true){
int a = bis1.read();
int b = bis2.read();
if(a != b){
return false;
}
if(a == -1){
return true;
}
}
} catch (Exception e) {
//Do something with exception
}
return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow