ios文件处理(二)

通过plist文件存取文件

在 ios文件处理(一)的项目中,修改HomeViewController.m的viewDidLoad方法

 - (void)viewDidLoad

{/*
    NSString *fileName = [[self documentsPath] stringByAppendingPathComponent:@"content.txt"];
    
    //NSString *fileName = [[self tempPath] stringByAppendingPathComponent:@"content.txt"];
    
    [self writeToFile:@"苹果的魅力!" withFileName:fileName];
    
    NSString *fileContent = [self readFromFile:fileName];
    
    NSLog(fileContent);*/
    
    NSString *fileName = [[self tempPath] 
                          stringByAppendingPathComponent:@"content.txt"];
    [self writeToFile:@"我爱苹果!" withFileName:fileName];
    
    NSString *fileContent = [self readFromFile:fileName];
    

   //操作plist文件,首先获取在Documents中的contacts.plist文件全路径,并且把它赋值给plistFileName变量。 

    NSString *plistFileName = [[self documentsPath] 
                               stringByAppendingPathComponent:@"contacts.plist"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:plistFileName]) {

        //载入字典中 

        NSDictionary *dict = [[NSDictionary alloc] 
                              initWithContentsOfFile:plistFileName];
        

       //按照类别显示在调试控制台中 

        for (NSString *category in dict) {
            NSLog(category);
            NSLog(@"********************");
            
            NSArray *contacts = [dict valueForKey:category];
            
            for (NSString *contact in contacts) {
                NSLog(contact);
            }
        }
        [dict release];
    } else {//如果Documents文件夹中没有contacts.plist文件的话,则从项目文件中载入contacts.plist文件。
        NSString *plistPath = [[NSBundle mainBundle] 
                               pathForResource:@"contacts" ofType:@"plist"];
        
        NSDictionary *dict = [[NSDictionary alloc]
                              initWithContentsOfFile:plistPath];
        

       //写入Documents文件夹中 

        fileName = [[self documentsPath] stringByAppendingPathComponent:@"contacts.plist"];
        
        [dict writeToFile:fileName atomically:YES];
        
        [dict release];
    }
    
    [super viewDidLoad];
}

效果图:

 

 

P.S:

我们有时会用到绑定资源 (通常将项目中的资源叫绑定资源,他们都是只读的。如果我们想在应用程序运行的时候对这些资源进行读写操作,就需要将它们复制到应用程序文件夹中,比如Documents和tmp文件夹)

在 AppDelegate.m中添加一个方法即可

//复制绑定资源

//原理:我们首先获取应用程序的Documents文件夹的位置,然后在Documents中搜索通过该方法参数传递进来的文件名,其中包括文件名和扩展名。如果该文件不存在,则通过NSBundle类直接获取该绑定资源并将其复制到Documents文件夹中
- (void) copyBundleFileToDocumentsFolder:(NSString *)fileName
                           withExtension:(NSString *)ext{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory
                           stringByAppendingPathComponent:[NSString stringWithString:fileName]];
    filePath = [filePath stringByAppendingString:@"."];
    filePath = [filePath stringByAppendingString:ext];
    [filePath retain];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {
        NSString *pathToFileInBundle = [[NSBundle mainBundle]
                                        pathForResource:fileName ofType:ext];
        NSError *error = nil;
        bool success = [fileManager copyItemAtPath:pathToFileInBundle 
                                            toPath:filePath 
                                             error:&error];
        if (success) {
            NSLog(@"文件已复制");
        } else {
            NSLog([error localizedDescription]);
        }
    }
}

原文地址:https://www.cnblogs.com/hanjun/p/2743746.html