I have two xml files (a.xml and b.xml) in my file system (iPhone). Now I want to know if these files contain exactly the same data. Most of the time, this comparison would be true, because b.xml is the result of a copyItemAtPath: operation. Unless it is overwritten by newer information. What would be the most efficient way to compare these files?
我的文件系统(iPhone)中有两个xml文件(a.xml和b.xml)。现在我想知道这些文件是否包含完全相同的数据。大多数情况下,这种比较都是正确的,因为b.xml是copyItemAtPath:操作的结果。除非它被更新的信息覆盖。比较这些文件最有效的方法是什么?
- I could read the contents of the files as strings and then compare the strings
- I could parse the files and compare some key elements
- I guess there is a very blunt way, that doesn't need to interpret the file, but allows me to compare on a lower level.
我可以将文件的内容作为字符串读取,然后比较字符串
我可以解析文件并比较一些关键元素
我想有一种非常生硬的方式,不需要解释文件,但允许我在较低级别进行比较。
Any suggestion is very welcome.
任何建议都是非常受欢迎的。
Thanks ahead
Sjakelien
Update:
I ended up doing this:
我最终这样做了:
oldData = [NSData dataWithContentsOfFile:PathToAXML];
newData = [NSData dataWithContentsOfFile:PathToBXML];
and then compare it with:
然后将其与:
[newData isEqualToData:oldData];
The question is still: is that more efficient than:
问题仍然是:效率高于:
oldData = [NSString dataWithContentsOfFile:PathToAXML];
newData = [NSString dataWithContentsOfFile:PathToBXML];
[newData isEqualToString:oldData];
4 个解决方案
#1
You could also use:
你也可以使用:
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr contentsEqualAtPath:PathToAXML andPath:PathToBXML])
NSLog (@"File contents match");
else
NSLog (@"File contents do not match");
#2
One alternative to comparing the file contents is to track the file modification date -- if it's later than the date you created the copy, you might assume that it has been updated.
比较文件内容的另一种方法是跟踪文件修改日期 - 如果它晚于您创建副本的日期,您可能会认为它已更新。
See fileAttributesAtPath:traverseLink: in the NSFileManager
class.
请参阅NSFileManager类中的fileAttributesAtPath:traverseLink :.
#3
Is there a reason you don't want to do something like an MD5 hash of the two files and compare the results - here is an example of some code that seems pretty common
有没有理由你不想做两个文件的MD5哈希并比较结果 - 这里有一些代码似乎很常见的例子
Gravatar for Iphone? How do I generate a hexadecimal MD5 hash?
Iphone的Gravatar?如何生成十六进制MD5哈希?
#4
BOOL filesAreEqual = [[NSData dataWithContentsOfMappedFile:file1] isEqual:[NSData dataWithContentsOfMappedFile:file2]];
#1
You could also use:
你也可以使用:
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr contentsEqualAtPath:PathToAXML andPath:PathToBXML])
NSLog (@"File contents match");
else
NSLog (@"File contents do not match");
#2
One alternative to comparing the file contents is to track the file modification date -- if it's later than the date you created the copy, you might assume that it has been updated.
比较文件内容的另一种方法是跟踪文件修改日期 - 如果它晚于您创建副本的日期,您可能会认为它已更新。
See fileAttributesAtPath:traverseLink: in the NSFileManager
class.
请参阅NSFileManager类中的fileAttributesAtPath:traverseLink :.
#3
Is there a reason you don't want to do something like an MD5 hash of the two files and compare the results - here is an example of some code that seems pretty common
有没有理由你不想做两个文件的MD5哈希并比较结果 - 这里有一些代码似乎很常见的例子
Gravatar for Iphone? How do I generate a hexadecimal MD5 hash?
Iphone的Gravatar?如何生成十六进制MD5哈希?
#4
BOOL filesAreEqual = [[NSData dataWithContentsOfMappedFile:file1] isEqual:[NSData dataWithContentsOfMappedFile:file2]];