数据处理之文件读写

来源:互联网 发布:游戏王 游戏 知乎 编辑:程序博客网 时间:2024/06/05 18:39

一. 文件的路径有几种方式获取

  1. 比如直接从工程中拖拽出来的路径是当前工程文件夹的路径, 这个路径会随工程的移动而发生变化.
  2. 也可以通过NSBundle获取, bundle可以获取工程里的文件的路径, 这个路径不会随着工程的移动而变化.
  3. 如果要做手机的缓存, 或者收藏功能, 上面两个路径是不能办到的, 我们需要把这么较为隐私的内容放到手机的沙盒里进行保存.

二. 在沙盒文件夹里一般有三个大的文件夹

  1. Document文件夹: 用于存储用户的定一些信息, 比如收藏的内容, 设置信息, 所以在做收藏功能的时候, 一般是往这个文件夹里放
  2. Library文件夹: 这个文件夹一般是开发者使用, 保存程序员要保留的内容, Library文件夹还包括:
    ①Caches文件夹: 一般放缓存的信息, 比如网络加载的图片
    ②Preferences文件夹: NSUserDEfaults这个类的内容保存在这个文件夹里
  3. tmp文件夹: 存储临时文件

三. 写入本地数据

一般分为三个步骤:
1. 找到沙盒路径
2. 拼接一个用来显示文本内容的文件路径
3. 把数据写入

1.字符串写入本地数据

NSString *str = @"你好, 大连";

1.寻找沙盒路径:
参数1: 指定要去的文件夹, 这个枚举是电脑和首届通用的类, 如果要去Documents这个文件夹, 是枚举的66行, 如果要去caches文件夹, 是枚举的第70行.
参数2: 选择文件夹类型, 选择第一个, 用户类型
参数3: 绝对路径(YES), 这个路径是给系统用的, 通过这个路径可以找到对应的文件夹, 相对路径(NO), 这个路径是给开发者用的, 通过这个路径能知道要前往的文件夹, 这个路径是给开发者使用的

NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomaINMask, YES);NSString *path = [sandBox lastObject];

2.拼接一个用来显示文本内容的文件路径

NSString *docPath = [path stringByAppendingPathComponent:@"大连.txt"];

3.把字符串写入

[str writeToFile:docPath atomically: YES encoding:NSUTF8StringEncoding error:nil];]

参数1: 路径
参数2:是否保护文件
参数3:写入格式
4.读取本地字符串的内容(前两步加上第四步)

NSString *tempStr = [NSString stringWithContentsOfFile:docpath encoding:NSUTF8StringEncoding error:nil];

2.数组写入本地数据, 读取本地数据

NSArray *arr = @[@"1", @"2", @"3", @"4", @"5"];    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *path = [sandBox lastObject];    // 拼接文件路径    NSString *docPath = [path stringByAppendingPathComponent:@"arr.plist"];    // 把数组对象写入到本地    [arr writeToFile:docPath atomically:YES];    // 读取本地文件中的数据    NSArray *tempArr = [NSArray arrayWithContentsOfFile:docPath];

3.字典写入本地数据, 读取本地数据

 NSDictionary *dic = @{@"1":@"2", @"3":@"4"};    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *path = [sandBox lastObject];    //NSLog(@"%@", path);    NSString *docPath = [path stringByAppendingPathComponent:@"dic.plist"];    [dic writeToFile:docPath atomically:YES];    NSDictionary *tempDic = [NSDictionary dictionaryWithContentsOfFile:docPath];    //NSLog(@"%@", tempDic);

4. 复杂对象的写入

 Student *stu = [[Student alloc] init];    stu.name = @"张三";    stu.age = 1;    stu.sex = @"男";    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *path = [sandBox lastObject];    NSString *docPath = [path stringByAppendingPathComponent:@"张斌.plist"];    // 写入, 也叫归档, 也叫序列化    [NSKeyedArchiver archiveRootObject:stu toFile:docPath];    //NSLog(@"%@", docPath);    // 读取, 反归档, 反序列化    Student *backStu = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];    //NSLog(@"%@", backStu.name);

5.如果数组中保存复杂对象, 怎么处理?

    Student *stu1 = [[Student alloc] init];    stu1.name = @"张三";    stu1.age = 20;    stu1.sex = @"男";    Student *stu2 = [[Student alloc] init];    stu2.name = @"李四";    stu2.age = 20;    stu2.sex = @"女";    Student *stu3 = [[Student alloc] init];    stu3.name = @"王五";    stu3.age = 16;    stu3.sex = @"女";    Student *stu4 = [[Student alloc] init];    stu4.name = @"赵四";    stu4.age = 13;    stu4.sex = @"男";    Student *stu5 = [[Student alloc] init];    stu5.name = @"小红";    stu5.age = 16;    stu5.sex = @"女";    NSArray *stuArr = @[stu1, stu2, stu3, stu4, stu5];    // 如果数组里保存的是复杂对象, 可以直接对数组进行归档    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *path = [sandBox lastObject];    NSString *docPath = [path stringByAppendingPathComponent:@"StuArr.plist"];    [NSKeyedArchiver archiveRootObject:stuArr toFile:docPath];    NSLog(@"%@", docPath);    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];    [arr enumerateObjectsUsingBlock:^(Student *obj, NSUInteger idx, BOOL * _Nonnull stop) {        NSLog(@"%@", obj);    }];

创建一个文件夹, 在文件夹中创建一个.txt文件, 在其中存入一个字符串

NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];    //NSLog(@"%@", sandBox);    // 创建一个文件管理者    NSFileManager *manager = [NSFileManager defaultManager];    // 拼接一个文件夹路径    NSString *filePath = [sandBox stringByAppendingPathComponent:@"1001"];    // 创建文件夹    [manager createDirectoryAtPath: filePath withIntermediateDirectories:YES attributes:nil error:nil];    NSLog(@"%@", filePath);    NSString *str = @"asdf";    NSString *docPath = [filePath stringByAppendingPathComponent:@"nihao.text"];    [str writeToFile:docPath atomically:YES encoding:NSUTF8StringEncoding error:nil];  // 移除地址为docPath的文件      //[manager removeItemAtPath:docPath error:nil];
0 0