Advertisement
Hi,
I am trying to think of an efficient way to verify that I modified a file and only that file, this could include adding or removing a file. I keep trying to think of all the different 'what ifs" and now my head is swimming :-)
Would a hash table of values be the most efficient? Where the key is the file name and the value is the file content? or would this be unmanageable?
Thanks,
Cecilia
I am trying to think of an efficient way to verify that I modified a file and only that file, this could include adding or removing a file. I keep trying to think of all the different 'what ifs" and now my head is swimming :-)
Would a hash table of values be the most efficient? Where the key is the file name and the value is the file content? or would this be unmanageable?
Thanks,
Cecilia
Advertisement
Advertisement
-
Re: efficient way to compare file content?
Thu, November 2, 2006 - 4:41 PMI was thinking that maybe I should voice some of the considerations I have thought of:
* if one list has more elements than the second
* if the lists have the same number of elements, but have different keys
* if the above are combines; different sizes and different elements
I guess one way would be to grab the Enumeration of keys, put them into a list and then compare somehow. -
-
Re: efficient way to compare file content?
Thu, November 2, 2006 - 4:58 PMHi Cecilia,
I'm not sure what kind of application you are working on,
maybe you could use a version control system ( for example cvs,clearcase,....) to manage this.
If not, you could already optimize by remember the lastModified date of the file, and only compare when this has changed.
I'm not sure if keeping the file content in memory is a good idea as this does not scale very well, unless you're sure the number of files is small and do not contain a lot of data.
You could also copy the files into some temporary directory, compare the timestamps of the cached files ( java.io.File can provide you this info),...
Dirk
-
Re: efficient way to compare file content?
Fri, November 17, 2006 - 1:18 AMPresuming that you have the before and after images, you could do this:
1) create a "before" HashMap, with keys being the filename and value being the checksum of the file.
2) create an "after" HashMap with the same keys and values
3) if the size of the before and after keySets are different, then you know you have done something wrong.
4) if(before.equals(after)) then you know you've changed nothing. The equals method on HashMap will go through each entry to make sure they are the same.
5) if they aren't the same then before.keySet().equals(after.keySet()) will tell you if the files are the same ones (i.e. you didn't add a new one and remove an old one).
6) and if you get to this point it's just a matter of iterating through all the entries to see if one and only one has a different new value than old.
You may need to research how to get a checksum, but I know that ant does it (see the checksum task) so it must be possible...
-