沙盒路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 数据是写在Library->Preferences下
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSLog(@"%p",defaults1);
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
NSLog(@"%p",defaults2);

NSUserDefaults *defaults3 = [[NSUserDefaults alloc]init];
NSLog(@"%p",defaults3);

NSArray *tempArray = @[@"1",@"2"];
[defaults1 setObject:tempArray forKey:@"array"];

// 1、获取程序的Home目录
// NSString *homeDirectory = NSHomeDirectory();
// NSLog(@"path:%@", homeDirectory);

// 2、获取document目录
// NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentPath = [documentPaths objectAtIndex:0];
// NSLog(@"path:%@", documentPath);

// 3、获取Cache目录
// NSArray *CachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
// NSString *CachePath = [CachePaths objectAtIndex:0];
// NSLog(@"%@", CachePath);

// 4、获取Library目录
// NSArray *LibraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
// NSString *LibraryPath = [LibraryPaths objectAtIndex:0];
// NSLog(@"%@", LibraryPath);

// 5、获取Tmp目录
// NSString *tmpDir = NSTemporaryDirectory();
// NSLog(@"%@", tmpDir);

// 6、写入文件
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *docDir = [paths objectAtIndex:0];
// NSLog(@"%@",docDir);
// if (!docDir) {
// NSLog(@"Documents 目录未找到");
// }
// NSArray *contentArray = [[NSArray alloc] initWithObjects:@"内容",@"content",nil];
// NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
// [contentArray writeToFile:filePath atomically:YES];


// 7、读取文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
NSLog(@"%@",filePath);
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
NSLog(@"%@", array);


// 8、判断一个文件是否存在,传入全路径(fileExistsAtPath)
// // 创建文件管理器
// NSFileManager * fileManager = [NSFileManager defaultManager];
//
// NSString * documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
// NSString * filePath = [documents stringByAppendingPathComponent:@"test"];
//
// // 判断一个文件是否存在,传入全路径
// if ([fileManager fileExistsAtPath:filePath]) {
// NSLog(@"it is exit");
// }


// 9、在Documents里创建目录
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
// NSLog(@"documentsDirectory%@",documentsDirectory);
// NSFileManager *fileManager = [NSFileManager defaultManager];
// NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
// // 创建目录
// [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];


// 10、在目录下创建文件
// NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];
// NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];
// NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"];
// NSString *string = @"写入内容,write String";
// [fileManager createFileAtPath:testPath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
// [fileManager createFileAtPath:testPath2 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
// [fileManager createFileAtPath:testPath3 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];


// 11、获取目录列里所有文件名
// 两种方法获取:subpathsOfDirectoryAtPath 和subpathsAtPath
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
// NSLog(@"documentsDirectory%@",documentsDirectory);
// NSFileManager *fileManage = [NSFileManager defaultManager];
// NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
// NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];
// NSLog(@"%@",file);
// NSArray *files = [fileManage subpathsAtPath: myDirectory ];
// NSLog(@"%@",files);


// 12、fileManager使用操作当前目录
// //创建文件管理器
// NSFileManager *fileManager = [NSFileManager defaultManager];
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
// //更改到待操作的目录下
// [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
// //创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
// NSString * fileName = @"testFileNSFileManager.txt";
// NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];
// [fileManager createFileAtPath:fileName contents:array attributes:nil];

// 13、删除文件
// [fileManager removeItemAtPath:fileName error:nil];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end