获得磁盘上最常用文件夹的路径
我们知道,苹果上的应用程序都是运行在自己的沙盒中的,很少也没有足够的权限跟沙盒外面的文件资源打交道,一般一个应用的文件目录如下:
想要获得应用目录下的文件夹路径最常用的操作:
NSFileManager类的URLsForDirectory:inDomains: 实例方法允许你在IOS的文件系统中搜索指定的目录,特别是在你应用的沙箱中.此方法有两个参数:
URLsForDirectory:
此参数是你将要搜索的字典对象.为此参数传递一个NSSearchPath类型的目录字典.我将在稍后详细讨论此参数.
inDomains:
此参数指定你在哪儿搜索给定的目录.此参数必须是一NSSearchDomainMask的枚举值.
假设你要获得你应用的 Document 文件夹路径,你会发现是如此简单: - NSFileManager *fileManager = [[NSFileManager alloc] init];
- NSArray *urls = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
- if ([urls count] > 0) {
- NSURL *documentsFolder = urls[0];
- NSLog(@"%@",documentsFolder);
- }else{
- NSLog(@"Could not find the Documents folder");
- }
下面是 NSFileManager 类实例方法 URLsForDirectory:inDomains:的所有你可以传递的重要参数值:
URLsForDirectory
NSLibraryDirectory 标记应用的library文件夹.
NSCachesDirectory 标记caches文件夹,在之前解释说明过.
NSDocumentDirectory 标记document文件夹.
inDomains
NSUserDomainMask 标记对文件夹路径的搜索在当前用户文件夹下进行.在OS X中,此文件夹为~/.
如果你想获得 tmp 文件夹的路径,请像这样使用 C 函数 NSTemporaryDirectory( );
- NSString *tempDirectory = NSTemporaryDirectory();
- NSLog(@"Temp Directory = %@",tempDirectory);
- 对文件进行读写操作
- //写
- -(BOOL)writeText:(NSString *)paramText toPath:(NSString *)paramPath{
- return [paramText writeToFile:paramPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- }
- //读
- -(NSString *)readTextFromPath:(NSString *)paramPath{
- return [[NSString alloc] initWithContentsOfFile:paramPath encoding:NSUTF8StringEncoding error:nil];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MyFile.txt"];
- if ([self writeText:@"Hello,World!" toPath:filePath]) {
- NSString *readText = [self readTextFromPath:filePath];
- if ([readText length]) {
- NSLog(@"Text read from disk = %@",readText);
- }else{
- NSLog(@"Failed to read the text from disk");
- }
- }else{
- NSLog(@"Failed to write the file");
- }
- }
- NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MyFile.txt"];
- NSArray *arrayOfNames = @[@"Steve",@"John",@"Aaron"];
- if ([arrayOfNames writeToFile:filePath atomically:YES]) {
- NSArray *readArray = [[NSArray alloc] initWithContentsOfFile:filePath];
- if ([readArray count]) {
- NSLog(@"Read the array back from disk just fine");
- }else{
- NSLog(@"Failed to read the array back from disk");
- }
- }else{
- NSLog(@"Failed to save the array to disk");
- }
NSArray类的实例方法writeToFile:atomiclly只能保存包含如下对象类型的数组:
NSString
NSDictionary
NSArray
NSData
NSNumber
NSDate
如果你试图在数组中插入其他的对象,则数据将无法被保存到磁盘。
字典具有和数组类似的进行数据写磁盘及读数据回字典对象的方式.方法名称也完全 相同,且数组的保存规则同样适用于字典
- NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MyFile.txt"];
- NSDictionary *dict = @{@"firstName": @"Aaron",
- @"lastName":@"zheng",
- @"age":@"21"};
- if ([dict writeToFile:filePath atomically:YES]) {
- NSDictionary *readDictionary = [[NSDictionary alloc] initWithContentsOfFile:filePath];
- if ([readDictionary isEqualToDictionary:dict]) {
- NSLog(@"The file we read is the same one as the one we saved");
- }else{
- NSLog(@"Failed to read the dictionary from disk");
- }
- }else{
- NSLog(@"Failed to write the dictionay to disk");
- }
- 在磁盘中创建一个文件夹:
- //在磁盘中创建一个文件夹
- -(void)createDirectory{
- NSFileManager *fileManager = [[NSFileManager alloc] init];
- NSString *tempDir = NSTemporaryDirectory();
- NSString *path = [tempDir stringByAppendingPathComponent:@"images"];
- NSError *error;
- if ([fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
- NSLog(@"Successfully created the directory");
- }else{
- NSLog(@"Failed to create the directory , ERROR = %@",error);
- }
- }
上面的 withIntermediateDirectories:YES 参数设置为YES,则在创建最深层文件夹的时候,若父文件夹不存在,系统会直接帮你创建好。
- 删除文件及文件夹
- //删除文件及文件夹
- -(void)removeDir{
- NSFileManager *fileManager = [[NSFileManager alloc] init];
- NSString *tempDir = NSTemporaryDirectory();
- NSString *path = [tempDir stringByAppendingString:@"images"];
- NSError *error;
- if([fileManager removeItemAtPath:path error:&error] == NO){
- NSLog(@"Failed to remove path %@, ERROR = %@",path,error);
- }
- }
好了,就先写到这里吧,,不想写了,有时间在写写关于磁盘中文件的安全处理,附注:文中的示例都是摘自《ios 6 Programming Cookbook》一书,由DevDiv网友自发组织翻译,支持技术和信息的共享!
沒有留言:
張貼留言