iOS开发中对文件目录的访问及管理的基本方法小结

2020-01-14 16:45:05于丽

下面来看一些常用的文件目录管理方法
1、常见的NSFileManager文件方法

复制代码
-(NSData *)contentsAtPath:path  //从一个文件读取数据

 


-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr  //向一个文件写入数据


-(BOOL)removeItemAtPath:path error:err  //删除一个文件


-(BOOL)moveItemAtPath:from toPath:to error:err  //重命名或者移动一个文件(to不能是已存在的)

-(BOOL)copyItemAtPath:from toPath:to error:err  //复制文件(to不能是已存在的)


-(BOOL)contentsEqualAtPath:path andPath:path2  //比较两个文件的内容


-(BOOL)fileExistAtPath:path  //测试文件是否存在


-(BOOL)isReadableFileAtPath:path  //测试文件是否存在,并且是否能执行读操作  


-(BOOL)isWriteableFileAtPath:path  //测试文件是否存在,并且是否能执行写操作  


-(NSDictionary *)attributesOfItemAtPath:path error:err  //获取文件的属性  


-(BOOL)setAttributesOfItemAtPath:attr error:err  //更改文件的属性


2.使用目录 复制代码
-(NSString *)currentDirectoryPath  //获取当前目录

 


-(BOOL)changeCurrentDirectoryPath:path  //更改当前目录


-(BOOL)copyItemAtPath:from toPath:to error:err  //复制目录结构(to不能是已存在的)


-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attribute:attr  //创建一个新目录


-(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag  //测试文件是不是目录(flag中储存结果YES/NO)


-(NSArray *)contentsOfDirectoryAtPath:path error:err  //列出目录内容


-(NSDirectoryEnumerator *)enumeratorAtPath:path  //枚举目录的内容


-(BOOL)removeItemAtPath:path error:err  //删除空目录