iOS学习笔记-----文件操作

一.沙盒

1.沙盒的概念

(1)iOS中每个App应用程序都有一个单独封闭的文件夹,这个文件夹称为沙盒 (sandbox)
(2)沙盒目录用来存放App的本地文件,例如:音频、视频、图片文件……..
(3)当前App应用程序没权限访问其他App的沙盒。(更安全)
(4)沙盒目录中有如下子目录:
- Documents :存放长期使用的文件.
- Library :系统存放文件.
- tmp :临时文件,App重启时,该目录下的文件清空.

2.获取沙盒的路径

文件是需要通过路径去访问.


        // 沙盒路径---方法一
        NSString *sandBoxPath = NSHomeDirectory();
        NSLog(@"沙盒路径:%@", sandBoxPath);

        // 拼接路径方法一
        NSString *path1 = [sandBoxPath stringByAppendingString:@"/Documents"];
        NSLog(@"path1:%@", path1);

        // 拼接路径方法二
        NSString *path2 = [sandBoxPath stringByAppendingPathComponent:@"Documents"];
        NSLog(@"path2:%@", path2);

        // 沙盒路径---方法二,此方法不仅在iOS开发可以用,在mac开发下也可用
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSLog(@"paths:%@", paths);

二.文件操作

1.文件的创建

NSFileManager *fileManager = [NSFileManager defaultManager];
       // NSHomeDirectory 在命令行模式是当前用户主目录
        NSString *homePath = NSHomeDirectory();
        NSLog(@"homePath:%@", homePath);

        NSString *str = @"好好学习,天天向上!";

        // 拼接路径
        NSString *filePath = [homePath stringByAppendingPathComponent:@"Desktop/file.txt"];

        // 把NSString -----> NSData
        NSData *dataContents = [str dataUsingEncoding:NSUTF8StringEncoding];

        // 把NSData写入硬盘
        // 实例化NSFileManager

        // 创建文件所调用的方法
        BOOL success = [fileManager createFileAtPath:filePath contents:dataContents attributes:nil];
        if (success) {

            NSLog(@"文件创建成功或者已经存在");
        }

2.文件夹的创建

      // 绘制路径
        NSString *dirPath = [homePath stringByAppendingPathComponent:@"Desktop/hua/yeah/ok"];
     NSFileManager *fileManager = [NSFileManager defaultManager];    
        success = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];

        if (success) {

            NSLog(@"文件夹创建成功或者已经存在");
        }

3.读取文件

        NSFileManager *fileManager = [NSFileManager defaultManager];    

        NSString *filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/file.txt"];

        NSData *fileData = [fileManager contentsAtPath:filePath2];

        NSString *str2 = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];

        NSLog(@"str2:%@", str2);

4.剪切文件

        NSFileManager *fileManager = [NSFileManager defaultManager];    

        NSString *sourcePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/file.txt"];
        NSString *dirPath2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/hua/file.txt"];

        BOOL success = [fileManager moveItemAtPath:sourcePath toPath:dirPath2 error:nil];

        if (success) {

            NSLog(@"文件剪切成功");
        }

5.复制文件 如果目标文件已经存在,则剪切和复制都不会成功

        NSFileManager *fileManager = [NSFileManager defaultManager];    
        NSString *dirPath3 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/file.txt"];
        NSString *sourcePath2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/hua/file.txt"];

        BOOL success = [fileManager copyItemAtPath:sourcePath2 toPath:dirPath3 error:nil];

        if (success) {

            NSLog(@"文件复制成功");
        }*/

6.删除文件

        NSFileManager *fileManager = [NSFileManager defaultManager];    

        NSString *sourcePath3 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/file.txt"];

        BOOL success =[fileManager removeItemAtPath:sourcePath3 error:nil];

        if (success) {

            NSLog(@"文件删除成功");
        }

7.读取文件属性

        NSFileManager *fileManager = [NSFileManager defaultManager];    

        NSString *sourcePath4 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/hua/file.txt"];
        NSDictionary *dic = [fileManager attributesOfItemAtPath:sourcePath4 error:nil];
        NSLog(@"dic:%@", dic);

三.文件读写

      // 1.NSString 写入文件
        NSString *str = @"同学们太棒了";

        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/string.txt"];

        // atomically:控制重写文件是否保护,YES:会确认写入的安全性,NO:会直接覆盖源文件
        BOOL success =[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

        if (success) {

            NSLog(@"写入数据成功");
        }


        // 2.NSArray 写入文件
        NSArray *array = @[@"huang", @"rui", @"hua"];
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/array.plist"];

        BOOL success = [array writeToFile:path atomically:YES];

        if (success) {

            NSLog(@"写入数据成功");
        }


        // 3.NSDictionary 写入文件
        NSDictionary *dic = @{
                              @"key" : @"value",
                              @"liuzhengyi" : @"xueba",
                              @"lilong" : @"jingshenhao",
                              @"tangyupeng" : @"nanshen"
                              };


        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/dictionary.plist"];
        BOOL success = [dic writeToFile:path atomically:YES];
        if (success) {

            NSLog(@"写入数据成功");
        }
         */

        // 4.读取文件
        // 读取文本
        NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/string.txt"];
        NSString *str = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"str:%@", str);

        // 读取数组文件(plist)
        NSString *path2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/array.plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path2];
        NSLog(@"array:%@", array);

        // 读取字典文件(plist);

        NSString *path3 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/dictionary.plist"];
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path3];
        NSLog(@"dic:%@", dic);

你可能感兴趣的:(iOS学习笔记)