Sandbox: 沙盒
1.什么是沙盒
iOS操作系统中,一个独立、封闭和安全的空间/ 文件系统,空间称为沙盒。
2.作用
在沙盒中创建文件夹/文件;存放下载资源(网页;图片;音频文件等等)
3.沙盒结构
3.1 bundel容器和数据容器的区别:前者只读权限;后者有可读和可写权限
a. Bundel Container(bundel 容器):存最终的.app包(可执行文件 + Assert + Info.plist等)
b. Data Container(数据容器) 创建/ 下载等等
/Documents: 程序运行过程产生的文件,并且需要备份(iTunes;iCloud)
/Library: 程序运行过程产生的文件,并且不需要备份;
下载资源
/Library/Caches: 下载的所有资源文件
/Library/Preferences: 配置文件: xxx.plist
/tmp: 一般不用(系统会定时不定时地删除)
备注: tmp是temporary单词的缩写
Demo1_DataContainer_Path
<span style="font-size:14px;">
<span style="color:#009900;">/**
* 使用代码的方式获取数据容器的三个文件夹所在的路径
*/</span>
<span style="color:#009900;">//1.获取沙盒数据容器根目录</span>
NSString *homePath = NSHomeDirectory();
NSLog(@"home根目录:%@", homePath);
<span style="color:#009900;">//2.获取Documents路径
/*参数一:指定要搜索的文件夹枚举值
参数二:指定搜索的域Domian: NSUserDomainMask
参数三:是否需要绝对/全路径:NO:波浪线~标识数据容器的根目录; YES(一般制定): 全路径
*/</span>
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];<span style="color:#009900;">//因为是iOS每个应用容器对应一个沙盒,所以firstObject或lastObject都可以</span>
<span style="color:#009900;">//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//NSString *documentsDirectory = [paths objectAtIndex:0]; //此数组里只有一个元素,所以firstObject和lastObject都可以</span>
NSLog(@"documents路径:%@", documentsPath);
<span style="color:#009900;">//3.获取Library路径</span>
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"library路径:%@", libraryPath);
<span style="color:#009900;">//4.获取tmp路径</span>
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"tmp路径:%@", tmpPath);
</span>
Demo02_ContentToFile
<span style="font-size:14px;">//需求:NSArray数据存到XXX/Documents/test.txt
//1.NSArray
NSArray *array =@[@"Jonny",@19, @[@"Objective-C",@"Swift", @"Ruby"]];
//2.Documents路径
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)firstObject];
//3.文件绝对路径
//3.1 自动添加“/”的字符串拼接方法
NSString *filePath = [documentPathstringByAppendingPathComponent:@"test.txt"];
//4.写入文件中
/*参数一:制定写入文件的绝对路径
参数二:是否是原子(YES:保证要么写成功,要么什么都没有;NO:没有临时文件,不能保证绝对写成功)
注意点:不用创建文件;plist格式;如果该文件没有,自动创建,如果有,直接写入(后写入的内容会覆盖原来的内容)
*/
if (![arraywriteToFile:filePath atomically:YES]) {
NSLog(@"文件写入失败");
}
/**
* 从指定的路径下读取文件的数据
* 原则:写入什么什么类型,需要用该类型接收
*/
NSArray *arrayFromFile = [NSArrayarrayWithContentsOfFile:filePath];</span>
总结:
1.文件格式:都是plist格式(属性列表)
2.拼接文件的绝对路径
文件管理类:NSFileManager
作用:做任何文件夹和文件的操作
使用:单例 + 方法
Demo03_NSFileManger
<span style="font-size:14px;">// 需求一:创建xxx/Documents/test文件夹
//1.拼接文件的路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *testDirPath = [documentsPath stringByAppendingPathComponent:@"test"];
//2.获取NSFileManager单利对象(shared/default/standard)
NSFileManager *fileMgr = [NSFileManager defaultManager];
//3.创建文件夹
/*
参数一:给定文件所在的路径
参数二:YES(一般):允许创建的文件存在;NO:不允许存在(如果存在报错)
参数三:指定创建文件夹的属性(权限+用户所有者+更新时间...);一般给nil(默认的文件夹属性)
参数四:返回错误
*/
NSError *error = nil;
if (![fileMgr createDirectoryAtPath:testDirPath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"创建文件夹失败:%@", error.userInfo);
}
//需求二:test/test01.txt; test/test02.txt; 并写入内容
//1.拼接两个文件路径
NSString *firstFilePath = [testDirPath stringByAppendingPathComponent:@"test01.txt"];
NSString *secondFilePath = [testDirPath stringByAppendingPathComponent:@"test02.txt"];
//2.创建两个文件,指定写入的内容
/*参数三:指定创建文件属性(权限 + 用户); 一般给nil(默认属性)*/
NSString *firstContent = @"写入第一个文件的内容。。。";
[fileMgr createFileAtPath:firstFilePath contents:[firstContent dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSString *secondContent = @"写入第二个文件的内容。。。";
[fileMgr createFileAtPath:secondFilePath contents:[secondContent dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
//需求三:test01.txt -> copy.txt
//1.拼接copy.txt路径
NSString *copyPath = [testDirPath stringByAppendingPathComponent:@"copy.txt"];
//2.拷贝操作(默认该方法会创建copy.txt文件;如果文件存在会报错)
//如果copy.txt不存在
if (![fileMgr fileExistsAtPath:copyPath]) {
if (![fileMgr copyItemAtPath:firstFilePath toPath:copyPath error: &error]) {
NSLog(@"拷贝失败:%@", error.userInfo);
}
}
//需求四:test文件夹下的所有文件(包含子文件夹...).
NSArray *subArray = [fileMgr subpathsAtPath:testDirPath];
NSLog(@"第一种方式返回的数组:%@", subArray);
NSArray *subSecondArray = [fileMgr subpathsOfDirectoryAtPath:testDirPath error:&error];
NSLog(@"第二种方式返回的数组:%@", subSecondArray);
//只返回当前文件夹下的一级目录以及文件.
NSArray *contentArray = [fileMgr contentsOfDirectoryAtPath:testDirPath error:&error];
NSLog(@"返回test文件夹下的所有内容:%@", contentArray);</span>