一.沙盒
1.沙盒的概念
(1)iOS中每个App应用程序都有一个单独封闭的文件夹,这个文件夹称为沙盒 (sandbox)
(2)沙盒目录用来存放App的本地文件,例如:音频、视频、图片文件……..
(3)当前App应用程序没权限访问其他App的沙盒。(更安全)
(4)沙盒目录中有如下子目录:
- Documents :存放长期使用的文件.
- Library :系统存放文件.
- tmp :临时文件,App重启时,该目录下的文件清空.
2.获取沙盒的路径
文件是需要通过路径去访问.
// 沙盒路径---方法一
NSString *sandBoxPath = NSHomeDirectory();
NSLog(@"沙盒路径:%@", sandBoxPath);
// 拼接路径方法一
NSString *path1 = [sandBoxPath stringByAppendingString:@"/Documents"];
NSLog(@"path1:%@", path1);
// 拼接路径方法二
NSString *path2 = [sandBoxPath stringByAppendingPathComponent:@"Documents"];
NSLog(@"path2:%@", path2);
// 沙盒路径---方法二,此方法不仅在iOS开发可以用,在mac开发下也可用
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"paths:%@", paths);
二.文件操作
1.文件的创建
NSFileManager *fileManager = [NSFileManager defaultManager];
// NSHomeDirectory 在命令行模式是当前用户主目录
NSString *homePath = NSHomeDirectory();
NSLog(@"homePath:%@", homePath);
NSString *str = @"好好学习,天天向上!";
// 拼接路径
NSString *filePath = [homePath stringByAppendingPathComponent:@