NSFileManager 常用操作
- -(BOOL)contentsAtPath:path //从一个path所指定的文件上读取数据
- -(BOOL)createFileAtPath:path contents:(NSData*)data attributes:attr //向一个path所指向的文件上写入数据data
- -(BOOL)removeFileAtPath:path handler:handler //删除一个path所指定的文件
- -(BOOL)movePath:from toPath:to handler:handler //重命名或移动一个文件。from是源文件,to是目标文件
- -(BOOL)contentsEqualAtPath:path1 andPath:path2 //比较两个文件的内容是相同
- -(BOOL)fileExistsAtPath:path //判断path所指定的文件是否存在
- -(BOOL)isReadableFileAtPath:path //判断path所指定的文件是否存在,能否进行读取操作
- -(BOOL)isWritableFileAtPath:path //判断path所指定的文件是否存在,能否进行写入操作
- -(NSDictionary*)fileAttributesAtPath:path traverseLink:(BOOL)flag //获取path所指定的文件的属性,返回一个字典类型
- -(BOOL)changeFileAtributes:attr atPath:path //更改path所指定的文件的属性
- NSFileManager目录操作
- -(NSString*)currentDirectoryPath //获取当前目录
- -(BOOL)changeCurrentDirectoryPath:path //更改当前目录
- -(BOOL)copyPath:from toPath:to handler:handler //复制目录结构
- -(BOOL)createDirectoryAtPath:path attributes:attr //创建一个新的目录
- -(BOOL)fileExistsAtPath:path isDirectory:(BOOL*)flag //判断是不是目录
- -(NSArray*)directoryContentsAtPath:path //列出目录内容
- -(NSDirectoryEnumerator*)enumeratorAtPath:path //枚举目录的内容
- -(BOOL)removeFielPath:path handler:handler //删除一个空目录
- -(BOOL)movePath:from toPath:to handler:handler //重命名或移动一个目录
- -(NSString*)NSUserName //返回当前用户目录
- -(NSString*)NSFullUserName //返回当前用户的完整目录
- -(NSString*)NSHomeDirectory //返回当前用户主目录路经
- -(NSString*)NSHomeDirectoryForUser:(NSString*)user //返回user用户的主目录
- -(NSString*)NSTemporaryDirectory //返回可以用于创建临时文件的临时目录
- +(NSString*)pathWithComponents:components //根据components中的值构造路经
- -(NSArray*)pathComponents //柝分路经,获得各个部分,并放入数组
- -(NSString*)lastPathComponents //提取路经中最后一个组成部分(一般就是文件名)
- -(NSString*)pathExtension //提取文件的扩展名
- -(NSString*)stringByAppendingPathComponents:path //将path增加到现有路经的尾部
- -(NSStirng*)stringByAppendingPathExtension:ext //将ext扩展名增加到尾部
- -(NSString*)stringByDeletingLastPathComponents //删除路经的最后一个组成部分
- -(NSString*)stringByDeletingPathExtension //删除最后的扩展名
- -(NSString*)stringByExpandingTildeInPath //将路经中的各个"~"符号转换为用户主目录(~)或者为一个指定用户的主目录(~user)
- -(NSString*)stringByStandardizingPath //解析~,父日目录(..)、当前目录(.)和符号链接来返回一个标准化路经
NSFileManager
每个路径都是一个NSString对象,可以是相对路径,也可以是绝对路径.
文件方法:
- (NSData *)contentsAtPath:(NSString *)path
从一个文件中读取数据.
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)contents attributes:(NSDictionary *)attributes
向一个文件中写入指定属性的内容.
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
删除一个文件,文件夹,链接
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
移动(重命名)文件到一个指定的路径
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
复制一个文件到指定的路径
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2
比较两个文件是否相同
- (BOOL)fileExistsAtPath:(NSString *)path
测试文件是否存在
- (BOOL)isReadableFileAtPath:(NSString *)path
测试文件是否存在并可读
- (BOOL)isWritableFileAtPath:(NSString *)path
测试文件是否存在并可写
- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error
获取文件属性
- (BOOL)setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error
更改文件属性