iOS NSFileManager

今天,用到了文件的管理,发现自己又忘得差不多了。屋里有个苍蝇,老是在眼前晃来晃去,好是烦人。

 

  用到了两个地方:

  1. 创建文件夹;

  2. 移动文件

 

功能还有很多,今天先总结两个!

  1. 创建文件夹;

 //测试是否是目录
    BOOL isDirectory;
    
    //因为第2个参数是BOOL *,是一个指针,而不是一个值。
    NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]
stringByAppendingPathComponent:@"abc.txt"]; BOOL isExist = [[NSFileManager defaultManager]fileExistsAtPath:path isDirectory:&isDirectory]; NSLog(@"isExist = %d----isDirectory = %d",isExist,isDirectory); if (!isExist & !isDirectory) { //创建新的文件夹 BOOL createSuccess = [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:YES
attributes:nil error:nil]; NSLog(@"createSuccess = %d",createSuccess); }

  

 

  2. 移动文件

-(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)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            //解析~,父日目录(..)、当前目录(.)和符号链接来返回一个标准化路经 

  

  

 

posted @ 2015-08-24 23:38  羊羊羊🐑  Views(154)  Comments(0Edit  收藏  举报