Documents目录下得增删改查

//在documents目录下创建一个名字为myFile的文件夹

  1. NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"myFile"];   

  2. NSLog(@"%@",path); 

//返回文件管理的一个单例

3. NSFileManager *fileManager = [NSFileManager defaultManager];      

4.  BOOL isDir = FALSE;      

5.  BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];

6.     if(!(isDirExist && isDir))          

{   BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];  

  if(!bCreateDir){            

    NSLog(@"创建文件夹失败!");        

}

   NSLog(@"创建文件夹成功,文件路径%@",path);    }

7.   UIImage *image=[UIImage imageWithContentsOfFile:@"myImage.png"];

   NSData *data = UIImagePNGRepresentation(image);

8.    //将myImage.png保存到myFile文件夹下,新名称为image.png

9.       BOOL isSaved=   [fileManager createFileAtPath:[path stringByAppendingString:@"/image.png"] contents:data attributes:nil];

10.     NSLog(@"图片保存状态:%d",isSaved);

11. //获取保存的图片    

UIImage *newimage=[UIImage imageWithContentsOfFile:[path stringByAppendingString:@"/image.png"]];

  12 //   删除文件

     BOOL isDelete=[fileManager removeItemAtPath:path error:nil];

    NSLog(@"%d",isDelete);

posted @ 2015-08-26 11:19  王 彪  阅读(387)  评论(0编辑  收藏  举报