iOS 的目录操作基础

1、文件管理类:NSFileManager

     常用接口:

     1.1 fileExistsAtPath 判断文件是否存在

     1.2 fileAttributesPath 判断文件夹属性,可读/可写

     1.3 copyPath 从一个目录拷贝一个文件到另外一个目录

     1.4 movePath 从一个目录转到文件到另外一个目录

     1.5 removeFileAtPath 删除指定目录中的文件

 

 

2、iOS可操作目录有两种:Documents下与Caches下, 如图:
iOS对于目录的操作

iOS对于目录的操作
1、创建目录createDirectoryAtPath:withIntermediateDirectories:attributes:error:

NSFileManager * fileManager = nil;

NSArray *paths = nil;

NSString *documentsDirectory = nil;

NSString * folerName = @"Photos";

NSString * fileName = @"myphoto.png";

NSString  * filePath = nil;

UIImage *photoimage = nil;

NSData * imageData = nil;

 

//Documents:

 

paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);


//Caches:

 

paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);


documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:folerName];

fileManager = [[NSFileManager alloc]init];

[fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:error];

 

 

2、创建目录并在目录中存储对象createFileAtPath: contents: attributes

 

filePath = [documentsDirectory stringByAppendingPathComponent:filename];

 

if((![fileManager fileExistsAtPath: fullPathToFile])) 

     {

photoimage = [[UIImage alloc] imageNamed:@"photo.png"];

imageData = UIImagePNGRepresentation(photoimage);

[fileManager createFileAtPath:filePath contents: imageData attributes:nil];

    }

[ photoimage release];

 

[fileManager release];

 

 

3、删除目录中指定文件

 

NSString * filePath = [documentsDirectory stringByAppendingPathComponent: fileName];

[filemanager removeItemAtPath: filePath error:NULL];

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值