Gavin.han

致力于移动开发 技术改变生活
随笔 - 133, 文章 - 0, 评论 - 46, 阅读 - 42万

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

ios文件处理(二)

Posted on   gavin.han  阅读(492)  评论(0编辑  收藏  举报

通过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]);
        }
    }
}

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示