NSFileManager文件管理者

   iOS的沙盒机制,应用只能访问自己应用目录下的文件。iOS不像android,没有SD卡概念,不能直接访问图像、视频等内容。iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。

             

上面的完整路径为:用户->资源库->Application Support->iPhone Simulator->6.1->Aplications

Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;

Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。

tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。

               

APP  Sandbox

iOS怎么获取沙盒路径,怎么操作文件呢?看DEMO:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view.
 4     self.view.backgroundColor = [UIColor whiteColor];
 5     
 6     [self initData];
 7 
 8     
 9 }
10 
11 
12 #pragma mark  --NSFileManager 文件管理者
13 /**
14  * 1.能够创建文件夹, 创建 删除 赋值 移动
15    2.通过路径
16    3.
17  */
18 
19 -(void)initData{
20    
21     //文件管理者都是系统的一个单例对象
22     NSFileManager *fileManager = [NSFileManager defaultManager];
23     
24     //沙盒documents文件夹路径
25     NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
26     
27     //沙盒caches文件夹路径
28     NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
29     
30     //1.创建文件夹
31     NSString *path = [documentsPath stringByAppendingPathComponent:@"DownLoad/image"];
32     
33     /*
34              方法的调用者:文件管理者
35      参数1:想要创建文件夹的路径
36      参数2:YES(该路径下创建不存在的文件夹) NO(忽略不存在文件夹,不创建)
37      参数3:对所创建的文件夹的相关设置
38      参数4:错误信息
39      */
40 
41     [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
42     
43     NSLog(@"%@",path);
44     
45     
46     //2.判断是否存在某个文件,如果存在,删掉,不存在则创建
47     
48     NSString *filePath = [cachesPath stringByAppendingPathComponent:@"xiaopingguo.mp3"];
49     
50     //判断该路径下是否存在对应的文件
51     if ([fileManager fileExistsAtPath:filePath]) {
52         
53         //存在删除
54         [fileManager removeItemAtPath:filePath error:nil];
55         NSLog(@"文档存在,删除");
56     }
57     
58     else
59     {
60         //不存在 创建
61         [fileManager createFileAtPath:filePath contents:nil attributes:nil];
62         
63         NSLog(@"文件不存在,创建");
64         
65     }
66     
67     NSLog(@"%@",filePath);
68     
69     
70 
71     //3.赋值
72     
73     [fileManager copyItemAtPath:filePath toPath:[documentsPath stringByAppendingPathComponent:@"xiaopingguo.mp3"] error:nil];
74     NSLog(@"%@",documentsPath);
75     
76     
77     //4.移动
78     /*
79      参数1:文件的源路径
80      参数2:目的路径,需要拼接上文件名字
81      参数3:错误信息
82      */
83     
84     [fileManager moveItemAtPath:filePath toPath:[path stringByAppendingPathComponent:@"dabaojian.mp4"] error:nil];
85     
86     
87     //5.获取文件的子文件
88     NSArray *fileArray = [fileManager contentsOfDirectoryAtPath:documentsPath error:nil];
89     
90     NSLog(@"%@",fileArray);
91 
92     
93 }
原文地址:https://www.cnblogs.com/yyxblogs/p/4872794.html