• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Java to compare 2 ODS files  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I have a program which generates as ODS file as output. In order to test this, I have an expected output file which has been pre-created. How do I compare the two ODS files programmatically using Java? The ODS creation is done using ODFToolkit. Thanks
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends what you mean by "compare". If you just want to see if they have identical contents, then you do it the same way you would for any other file:

1. If the sizes are different, the files are different, and you're done.

2. If the sizes are the same, open one InputStream for each file, read, compare the bytes from one to the bytes from the other until a difference is found or you reach the end.

If you mean something more complex than that, something based on the semantics of whatever this ODS stuff is, then your first step is to define--precisely, thoroughly, and without regard to Java--what kind of comparison and how to do it. Note that this part has nothing at all to do with Java.
 
Amit Bhargava
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

I had tried using FileUtils.contentEquals, but that didn't work. Another approach would be to get the content Dom of the ods files and comparing the corresponding nodes. I can do that, but I'd like to use a library method if one exists.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Bhargava wrote:Hi Jeff,

I had tried using FileUtils.contentEquals


I've never heard of this.

, but that didn't work.


ItDoesntWorkIsUseless.(⇐click)
 
Saloon Keeper
Posts: 4038
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html

It does exactly what Jeff explained - a byte for byte comparison. You'll have to tell us how it didn't work.
 
Amit Bhargava
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies, Jeff and Tim.

I was trying out a JUnit test for the program I had written. This involves checking expected output versus the actual output. I tried it out as follows:
1) Create a blank ODS file as 'expected' output
2) Run the test
3) As expected, the 'actual' output does not match the 'expected' output
4) Replace the 'expected' output with the 'actual' output
5) Run the test
6) The 'expected' and 'actual' files are still not equal, even though the contents are identical

Essentially, some header info might be different in the two ODS files. Hence the byte-by-byte comparison fails.

Jeff, the FileUtils class is part of an Apache I/O library. The link to the API for the same is http://commons.apache.org/io/api-1.4/index.html?org/apache/commons/io/FileUtils.html

Thanks again, folks.



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Bhargava wrote:Thanks for the replies, Jeff and Tim.

I was trying out a JUnit test for the program I had written. This involves checking expected output versus the actual output. I tried it out as follows:
1) Create a blank ODS file as 'expected' output
2) Run the test
3) As expected, the 'actual' output does not match the 'expected' output
4) Replace the 'expected' output with the 'actual' output
5) Run the test
6) The 'expected' and 'actual' files are still not equal, even though the contents are identical


Then either:

1) There is a bug in your code.

OR

2) You're not looking at what you think you're looking at, and the results are actually correct.

OR

3) You're not looking at what you think you're looking at, and the files don't actually match.

If you want more detailed help than that, you'll need to TellTheDetails,(⇐click) preferably with an SSCCE(⇐click) that is representative of what you're doing and exhibits the problem you're having.

Essentially, some header info might be different in the two ODS files. Hence the byte-by-byte comparison fails.


The header is part of the file. If there's a difference, then you didn't copy the file correctly.

Or, if you want the files to be considered equal even with different headers, then you need code that will understand the ODS format well enough to be able to ignore the header differences. There's no reason to think that FileUtils or any other library other than one directly from the ODS provider made specifically for that purpose will be able to do this for you, so you'll most likely have to write that part yourself.

 
Amit Bhargava
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again, Jeff.

I DO want the files to be considered equal even if the header data is different. I guess I'll go ahead and code it myself. Thanks for your suggestions.
 
Amit Bhargava
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case anyone needs the solution, here it is

 
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look on this API:
http://code.google.com/p/java-diff-utils/

Very useful for your needs.
Hope this helps.
 
Tim Moores
Saloon Keeper
Posts: 4038
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Candido Volpato da Cunha wrote:http://code.google.com/p/java-diff-utils/
Very useful for your needs.

It says "comparison operations between texts" - how would that help compare streams or binary data?
 
Amit Bhargava
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bruno,
Thanks for the response. Unfortunately, that would not have solved my problem fully as the header data in the 2 ODS files would have still been different. A similar functionality is provided by the org.apache.commons.io.FileUtils.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!