[Objective-C] 013_文件系统(File System)
2015-07-19 23:12
591 查看
在前面三篇关于数据持久化,我们都用涉及到文件(plist文件,数据库文件),它们都是把它们存储在document目录下。iOS的文件机制是沙盒机制,应用只能访问自己应用目录下的文件。iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。
Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。
tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。
我们实际中要如何来或取沙盒,如何在沙盒里面进行相应的操作,下面一一分解。
1.获取app的沙盒根目录
2.获取Documents目录路径
3.获取Library(包含Caches、Preferences)目录路径:
4.获取tmp目录路径:
5.创建文件(tmp文件夹中)
6.创建文件夹
7.删除文件
8.写入文件
以上是iOS 文件的一些简单常见操作。更多详细内容请参考(https://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2)
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: /article/6669124.html
Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。
tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。
我们实际中要如何来或取沙盒,如何在沙盒里面进行相应的操作,下面一一分解。
1.获取app的沙盒根目录
NSString *appRootDir=NSHomeDirectory(); NSLog(@"我的沙盒路径根路径------》: %@",appRootDir);
2.获取Documents目录路径
//第一种
NSString *appRootDir=NSHomeDirectory(); NSLog(@"我的沙盒路径根路径------》: %@",appRootDir);NSString *documentDir = [appRootDir stringByAppendingPathComponent:@"Documents"];
NSLog(@"documentDir: -----》 %@",documentDir);
//第二种
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory: -----》 %@",documentsDirectory);
3.获取Library(包含Caches、Preferences)目录路径:
//第一种
NSString *appRootDir=NSHomeDirectory(); NSLog(@"我的沙盒路径根路径------》: %@",appRootDir);NSString *documentDir = [appRootDir stringByAppendingPathComponent:@"Library"];
NSLog(@"documentDir: -----》 %@",documentDir);
//第二种
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
NSLog(@"libraryDirectory: -----》 %@",libraryDirectory);
//Caches
NSString *cachesPath = [libraryDirectory stringByAppendingString:@"Caches"];
//Preferences
NSString *preferencesPath = [libraryDirectory stringByAppendingString:@"Preferences"];
NSLog(@"cachesPath: -----》 %@",cachesPath);
NSLog(@"preferencesPath: -----》 %@",preferencesPath);
4.获取tmp目录路径:
//第一种
NSString *appRootDir=NSHomeDirectory(); NSLog(@"我的沙盒路径根路径------》: %@",appRootDir);NSString *documentDir = [appRootDir stringByAppendingPathComponent:@"tmp"];
NSLog(@"documentDir: -----》 %@",documentDir);
//第二种
NSString *tmpDirectory = NSTemporaryDirectory();
NSLog(@"tmpDirectory: -----》 %@",tmpDirectory);
5.创建文件(tmp文件夹中)
NSString *tmpDirectory = NSTemporaryDirectory(); NSLog(@"tmpDirectory: -----》 %@",tmpDirectory); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testFilePath = [tmpDirectory stringByAppendingPathComponent:@"testFile.txt"]; BOOL res=[fileManager createFileAtPath:testFilePath contents:nil attributes:nil]; if (res) { NSLog(@"测试文件创建成功: %@" ,testFilePath); }else { NSLog(@"测试文件创建失败"); }
6.创建文件夹
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testFolderDirectory = [documentsPath stringByAppendingPathComponent:@"Test"]; // 创建目录 BOOL res=[fileManager createDirectoryAtPath:testFolderDirectory withIntermediateDirectories:YES attributes:nil error:nil]; if (res) { NSLog(@"Test文件夹创建成功"); }else { NSLog(@"Test文件夹创建失败"); }
7.删除文件
NSString *tmpDirectory = NSTemporaryDirectory(); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *testFilePath = [tmpDirectory stringByAppendingPathComponent:@"testFile.txt"]; //判断文件是否存在 if ([fileManager fileExistsAtPath:testFilePath]) { BOOL res=[fileManager removeItemAtPath:testFilePath error:nil]; if (res) { NSLog(@"testFile文件删除成功"); }else NSLog(@"testFile文件删除失败"); }
8.写入文件
NSString *tmpDirectory = NSTemporaryDirectory(); NSLog(@"tmpDirectory: -----》 %@",tmpDirectory); NSString *content=@"www.babybus.com SuperDo"; NSString *testFilePath = [tmpDirectory stringByAppendingPathComponent:@"testFile.txt"]; BOOL res=[content writeToFile:testFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (res) { NSLog(@"testFile文件写入成功"); }else { NSLog(@"testFile文件写入失败"); }
以上是iOS 文件的一些简单常见操作。更多详细内容请参考(https://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2)
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: /article/6669124.html
相关文章推荐
- WPF基础学习笔记(一)Dependency Object 和 Dependency Property
- 黑马程序员--Objective-C语言基础知识--构造方法和内存管理
- Objective-C学习笔记 复合
- Objective-C设计模式——工厂方法模式virtual constructor(对象创建)
- Objective-C设计模式——原型Prototype(对象创建)
- Objective-C中的instancetype和id区别
- 黑马程序员--Objective-C语言基础知识--特有语法
- Swift项目兼容Objective-c问题汇总
- Property follows Cocoa naming convention for returning 'owned' objects 错误解决办法
- Objective C中数组排序几种情况的总结
- 从Android到iOS开发——(1)、objective-c与java语法对比
- c++ objective c 混合编程问题总结
- objective-c UITableview 自定义滑操(原创)
- Android webview js 调用java方法报错"Uncaught TypeError: Object [object Object] has no method xx
- 黑马程序员--Objective-C语言基础知识--三大特性
- Objective - C学习 (Foundation框架)-- 02类
- Objective - C学习 (Foundation框架)-- 01结构体
- Object 类中的equals
- 13 Holding Your Objects课后练习
- WCF的配置参数maxItemsInObjectGraph所引发的的问题