iOS文件处理介绍(二) 通过plist文件存取文件
在 ios文件处理(一)的项目中,修改HomeViewController.m的viewDidLoad方法:[摘要]本文介绍iOS文件处理之通过plist文件存取文件,并提供简单的示例代码供参考。
1 | - (void)viewDidLoad |
2 | |
3 | {/* |
4 | NSString *fileName = [[self documentsPath] stringByAppendingPathComponent:@"content.txt"]; |
5 | |
6 | //NSString *fileName = [[self tempPath] stringByAppendingPathComponent:@"content.txt"]; |
7 | |
8 | [self writeToFile:@"苹果的魅力!" withFileName:fileName]; |
9 | |
10 | NSString *fileContent = [self readFromFile:fileName]; |
11 | |
12 | NSLog(fileContent);*/ |
13 | |
14 | NSString *fileName = [[self tempPath] |
15 | stringByAppendingPathComponent:@"content.txt"]; |
16 | [self writeToFile:@"我爱苹果!" withFileName:fileName]; |
17 | |
18 | NSString *fileContent = [self readFromFile:fileName]; |
19 | |
20 | //操作plist文件,首先获取在Documents中的contacts.plist文件全路径,并且把它赋值给plistFileName变量。 |
21 | |
22 | NSString *plistFileName = [[self documentsPath] |
23 | stringByAppendingPathComponent:@"contacts.plist"]; |
24 | |
25 | if ([[NSFileManager defaultManager] fileExistsAtPath:plistFileName]) { |
26 | //载入字典中 |
27 | |
28 | NSDictionary *dict = [[NSDictionary alloc] |
29 | initWithContentsOfFile:plistFileName]; |
30 | |
31 | //按照类别显示在调试控制台中 |
32 | |
33 | for (NSString *category in dict) { |
34 | NSLog(category); |
35 | NSLog(@"********************"); |
36 | |
37 | NSArray *contacts = [dict valueForKey:category]; |
38 | |
39 | for (NSString *contact in contacts) { |
40 | NSLog(contact); |
41 | } |
42 | } |
43 | [dict release]; |
44 | } else {//如果Documents文件夹中没有contacts.plist文件的话,则从项目文件中载入contacts.plist文件。 |
45 | NSString *plistPath = [[NSBundle mainBundle] |
46 | pathForResource:@"contacts" ofType:@"plist"]; |
47 | |
48 | NSDictionary *dict = [[NSDictionary alloc] |
49 | initWithContentsOfFile:plistPath]; |
50 | |
51 | //写入Documents文件夹中 |
52 | |
53 | fileName = [[self documentsPath] stringByAppendingPathComponent:@"contacts.plist"]; |
54 | |
55 | [dict writeToFile:fileName atomically:YES]; |
56 | |
57 | [dict release]; |
58 | } |
59 | |
60 | [super viewDidLoad]; |
61 | } |
效果图:
我们有时会用到绑定资源 (通常将项目中的资源叫绑定资源,他们都是只读的。如果我们想在应用程序运行的时候对这些资源进行读写操作,就需要将它们复制到应用程序文件夹中,比如Documents和tmp文件夹)
在 AppDelegate.m中添加一个方法即可
1 | //复制绑定资源 |
2 | |
3 | //原理:我们首先获取应用程序的Documents文件夹的位置,然后在Documents中搜索通过该方法参数传递进来的文件名,其中包括文件名和扩展名。如果该文件不存在,则通过NSBundle类直接获取该绑定资源并将其复制到Documents文件夹中 |
4 | - (void) copyBundleFileToDocumentsFolder:(NSString *)fileName |
5 | withExtension:(NSString *)ext{ |
6 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
7 | NSString *documentsDirectory = [paths objectAtIndex:0]; |
8 | NSString *filePath = [documentsDirectory |
9 | stringByAppendingPathComponent:[NSString stringWithString:fileName]]; |
10 | filePath = [filePath stringByAppendingString:@"."]; |
11 | filePath = [filePath stringByAppendingString:ext]; |
12 | [filePath retain]; |
13 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
14 | if (![fileManager fileExistsAtPath:filePath]) { |
15 | NSString *pathToFileInBundle = [[NSBundle mainBundle] |
16 | pathForResource:fileName ofType:ext]; |
17 | NSError *error = nil; |
18 | bool success = [fileManager copyItemAtPath:pathToFileInBundle |
19 | toPath:filePath |
20 | error:&error]; |
21 | if (success) { |
22 | NSLog(@"文件已复制"); |
23 | } else { |
24 | NSLog([error localizedDescription]); |
25 | } |
26 | } |
27 | } |