iOS对于目录的操作

原文地址:iOS对于目录的操作作者:angellixf
iOS可操作目录有两种:Documents下与Caches下, 如图:
[转载]iOS对于目录的操作 - 102212335 - 缘的博客

[转载]iOS对于目录的操作 - 102212335 - 缘的博客

 
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(NSDocumentDirectoryNSUserDomainMaskYES);

 

//Caches:

paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

 

documentsDirectory = [[paths objectAtIndex:0stringByAppendingPathComponent: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 allocimageNamed:@"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];
原文地址:https://www.cnblogs.com/xiaonanxia/p/2729239.html