您的位置:首页 > 移动开发 > IOS开发

iOS 的目录操作基础

2011-06-02 09:04 281 查看
1、文件管理类:NSFileManager

常用接口:

1.1 fileExistsAtPath 判断文件是否存在

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

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

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

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

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








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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: