检查Cocoa中的两个文件是否相同 [英] Check if two files are the same in Cocoa

查看:157
本文介绍了检查Cocoa中的两个文件是否相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何有效地检查Cocoa中两个文件是否相同(具有相同的数据)?

How do you efficiently check if two files are the same (have the same data) in Cocoa?

上下文:我正在编写一个接收文件的程序作为输入(输入文件),并将其复制到目录中。如果目录已经包含具有相同名称的文件(同名文件),则只有当该名称文件不同时,才应使用新名称复制输入文件。

Context: I'm writing a program that receives a file as input (input file) and copies it into a directory. If the directory already contains a file with the same name (namesake file) then the input file should be copied with a new name only if the namesake file is different.

推荐答案

可以使用 - [NSFileManager contentsEqualAtPath:andPath:]

文档:


如果path1和path2是目录,则内容是每个包含的文件和子目录的列表 - 还比较子目录的内容。对于文件,此方法检查它们是否是相同的文件,然后比较它们的大小,最后比较它们的内容。此方法不会遍历符号链接,但会比较链接本身。

If path1 and path2 are directories, the contents are the list of files and subdirectories each contains—contents of subdirectories are also compared. For files, this method checks to see if they’re the same file, then compares their size, and finally compares their contents. This method does not traverse symbolic links, but compares the links themselves.

这篇关于检查Cocoa中的两个文件是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆