1 #import "ViewController.h"
2
3 @interface ViewController ()
4
5 @end
6
7 @implementation ViewController
8
9 - (void)viewDidLoad {
10 [super viewDidLoad];
11 // Do any additional setup after loading the view, typically from a nib.
12
13 //获取沙盒路径 NSHomeDirectory()
14 NSLog(@"%@",NSHomeDirectory());
15
16 //获取应用程序包路径
17 NSLog(@"%@",[NSBundle mainBundle]);
18 //获取程序包里一个Info.plist文件路径的方法
19 NSString *InfoPath = [[NSBundle mainBundle] pathForResource:@"Info.plist" ofType:nil];
20 NSLog(@"%@",InfoPath);
21
22 //获取临时文件目录tmp
23 NSLog(@"%@",NSTemporaryDirectory());
24 //检索沙盒路径
25 //获取文档路径 Documents ,第二个参数是固定值,
26 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
27 NSString *path = [paths objectAtIndex:0];
28 NSLog(@"%@",path);
29
30 //获取library路径
31 NSString *library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
32 NSLog(@"%@",library);
33
34 //获取Caches路径
35 NSString *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
36 NSLog(@"%@",caches);
37
38
39 //创建字符串存储在caches目录下text.txt
40
41 NSString *textPath = [caches stringByAppendingPathComponent:@"text.txt"];
42 NSLog(@"%@",textPath);
43 NSString *str = @"你咋不上天呢!";
44 //写入文件
45 [str writeToFile:textPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
46 //读取字符数据,初始化新NSString对象
47 NSString *str2 = [NSString stringWithContentsOfFile:textPath encoding:NSUTF8StringEncoding error:nil];
48 NSLog(@"%@",str2);
49
50 //将字符串转换为数据,由于下面的语句data的数据来源于str2,所以二者的内容一样,同时str2将被覆盖掉
51 NSData *data = [str2 dataUsingEncoding:NSUTF8StringEncoding];
52 NSLog(@"%@",data);
53
54 [data writeToFile:textPath atomically:YES];
55
56 NSData *data2 = [NSData dataWithContentsOfFile:textPath];
57 NSLog(@"%@",data2);
58
59
60
61 //数组的存取
62 NSArray *array = @[@"王玲玲",@"原溢凯",@"宋铭仁",@"孟德峰",@"杜占军",@"王振"];
63 //创建文件路劲
64 NSString *arrayPath = [caches stringByAppendingPathComponent:@"array.plist"];
65 //数组存储
66 [array writeToFile:arrayPath atomically:YES];
67 //数组的读取
68 NSArray *array2 = [NSArray arrayWithContentsOfFile:arrayPath];
69 NSLog(@"%@",array2);
70
71
72 //字典
73 NSString *dictionaryPath = [caches stringByAppendingPathComponent:@"dictionary.plist"];
74
75 //name:宋铭仁 hobby:music gender:男
76 NSDictionary *dic = @{@"name":@"宋铭仁",@"hobby":@"music",@"geder":@"男"};
77 [dic writeToFile:dictionaryPath atomically:YES];
78 NSDictionary *dic2 = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];
79 NSLog(@"%@",dic2);
80
81
82 //文件管理器,单例类
83 NSFileManager *fileManager = [NSFileManager defaultManager];
84 //创建文件
85 //文件路径
86 NSString *filePath = [caches stringByAppendingPathComponent:@"file.txt"];;
87 [fileManager createFileAtPath:filePath contents:data attributes:nil];
88
89 //我们很少直接设置文件的属性,一般都是系统自己设定的
90 NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePath error:nil];
91 NSLog(@"%@",fileAttributes);
92
93
94 //读取文件内容,由于存的时候必须用NSData类型的,所以接收的时候也是NSData类型的
95 NSData *fileData = [fileManager contentsAtPath:filePath];
96 //将Data类型的转化为string类型的
97 NSString *string = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
98 NSLog(@"%@",string);
99
100
101 //文件移动,文件移动本质上相当于移动并且重命名,所以必须两个文件路径必须给全路径,目的路径不能只给到文件夹就算了
102 NSString *sourcePath = [caches stringByAppendingPathComponent:@"file.txt"];
103 NSString *destinationPath = [library stringByAppendingPathComponent:@"ABCD.txt"];
104 [fileManager moveItemAtPath:sourcePath toPath:destinationPath error:nil];
105
106 //文件的复制
107 NSString *copySrcPath = destinationPath;
108 NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
109 //移动,复制文件,srcPath源文件路径和dstPath目的文件路劲,都必须是完整路径
110 NSString *copyDstPath = [documents stringByAppendingPathComponent:@"EFGH.txt"];
111 [fileManager copyItemAtPath:copySrcPath toPath:copyDstPath error:nil];
112
113 //判断文件内容是否相同
114 BOOL isSame = [fileManager contentsEqualAtPath:copySrcPath andPath:copyDstPath];
115 NSLog(@"%@",isSame ? @"相同" :@"不同");
116
117 //判断文件是否存在
118 BOOL isExist = [fileManager fileExistsAtPath:filePath];
119 NSLog(@"%@",isExist ? @"存在" :@"不存在");
120
121 //删除文件
122 //[fileManager removeItemAtPath:copyDstPath error:nil];
123 //[fileManager removeItemAtPath:copySrcPath error:nil];
124
125 //创建文件夹
126 //withIntermediateDirectories 表示路径中是否包含子文件夹
127 NSString *directoryPath = [documents stringByAppendingPathComponent:@"宋铭仁"];
128 [fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
129 //ck/ck 的斜杠表示ck文件夹下还包含一个ck的子文件夹
130 NSString *intermediatePath = [documents stringByAppendingPathComponent:@"ck/ck"];
131 [fileManager createDirectoryAtPath:intermediatePath withIntermediateDirectories:YES attributes:nil error:nil];
132
133 }