iOS数据文件存储二

作者: 沉默学飞翔 | 来源:发表于2017-09-08 14:15 被阅读24次

    前言

    因为这一段时间有需求需要用到文件的存取操作,而之前基本上对于本地数据的存储就是简单数据用NSUserDefaults,复杂点的用归档,再复杂一点的用sql。对于文件存取了解不多。现在需求来了,没办法,只能脱了裤子就是...。
    文件存储分三节来说:

    正文

    NSFileManager

    NSFileManager 是iOS的文件管理的一个类,主要是对文件进行创建,删除,获取文件信息等操作。有一点要知道,NSFileManager的使用级别是文件,他不能对文件里面的内容做什么处理操作。你想修改一个txt里面的数据,对不起,NSFileManager帮助不了你的,这时候就需要NSFileHandle来救场,一会再说这个类。
    NSFileManager 里面的方法有很多,平常我(low👃程序🐶)使用到了基本上就几个:

    //创建一个文件,并给文件内容
    - (BOOL)createFileAtPath:(NSString *)path contents:(nullable NSData *)data attributes:(nullable NSDictionary<NSString *, id> *)attr;
    
    //创建一个文件夹
    - (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSString *, id> *)attributes error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
    
    //判断文件是否存在
    - (BOOL)fileExistsAtPath:(NSString *)path;
    - (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(nullable BOOL *)isDirectory;
    
    //文件的复制,移动,删除操作
    - (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
    - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
    - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
    

    举个栗子:

    //1 存储路径的获取
        NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file_test1.txt"];
        
        //2 创建文件需要一个文件管理对象
        NSFileManager *manager = [NSFileManager defaultManager];
        
        //3 创建文件
        NSString *testStr = @"这是一个文件测试";
        [manager createFileAtPath:path1 contents:[testStr dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    

    栗子标准了步骤,万年按照步骤走,就不会有事,思路最重要。
    可以看到,NSFileManager需要单例创建, 然后调用创建文件的方法

    - (BOOL)createFileAtPath:(NSString *)path contents:(nullable NSData *)data attributes:(nullable NSDictionary<NSString *, id> *)attr;
    

    这个方法需要传入的数据为NSData类型,所以要进行一次NSData的转换。
    到底创建成功了没有呢,我们去文件的路径查询一下
    效果:


    栗子一

    说明文件已经创建成功了。那么,我们怎么去查询这个文件的一些信息吗。这还不简单,点击文件->右键->显示简介,搞定了···。😢😢😢,出门左拐,不谢。
    正确姿势:

    //获取文件信息
        NSDictionary *dic = [manager attributesOfItemAtPath:path1 error:nil];
        
        [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
            NSLog(@"key = %@,value = %@",key,obj);
        }];
    

    然后输出获得的属性字典,查看所有的文件属性:

    2017-09-08 13:25:14.782 文件读写Demo[2532:368804] key = NSFileOwnerAccountID,value = 501
    2017-09-08 13:25:14.783 文件读写Demo[2532:368804] key = NSFileSystemFileNumber,value = 23349726
    2017-09-08 13:25:14.783 文件读写Demo[2532:368804] key = NSFileExtensionHidden,value = 0
    2017-09-08 13:25:14.783 文件读写Demo[2532:368804] key = NSFileSystemNumber,value = 16777220
    2017-09-08 13:25:14.783 文件读写Demo[2532:368804] key = NSFileSize,value = 24
    2017-09-08 13:25:14.783 文件读写Demo[2532:368804] key = NSFileGroupOwnerAccountID,value = 20
    2017-09-08 13:25:14.784 文件读写Demo[2532:368804] key = NSFilePosixPermissions,value = 420
    2017-09-08 13:25:14.786 文件读写Demo[2532:368804] key = NSFileCreationDate,value = 2017-09-08 05:25:14 +0000
    2017-09-08 13:25:14.786 文件读写Demo[2532:368804] key = NSFileType,value = NSFileTypeRegular
    2017-09-08 13:25:14.787 文件读写Demo[2532:368804] key = NSFileGroupOwnerAccountName,value = staff
    2017-09-08 13:25:14.787 文件读写Demo[2532:368804] key = NSFileReferenceCount,value = 1
    2017-09-08 13:25:14.787 文件读写Demo[2532:368804] key = NSFileModificationDate,value = 2017-09-08 05:25:14 +0000
    

    如果,我们文件创建成功了,但是却发现路径错了,那应该怎么办呢?不用怕,NSFileManager可以帮你。

    //6 移动后的文件路径位置
        NSString *resultPath = [cachesPath stringByAppendingPathComponent:[path lastPathComponent]];
        
        //7 移动文件
        [manager moveItemAtPath:path toPath:resultPath error:nil];
    

    这样,咱们的文件就被移动走了。
    其他的比如,查询文件是否存在之类的操作,同学们自己去写一下测试一下。如果只要看就都可以记住的话,那是你想的美。

    NSFileManager

    细心同学又发现了,如果我的文件内容写错了,怎么办,NSFileManager帮不了。不用怕,NSFileManager 还有一个表弟##NSFileHandle 可以很好的帮助你。
    NSFileHandle 又叫做文件对接器,针对的是文件内容的读写操作。通过NSdata对文件内容进行改变。
    NSFileManager 不向他表哥一样,方法不多。用到的基本有这几个:

    //对文件需要做哪一类操作
    + (nullable instancetype)fileHandleForReadingAtPath:(NSString *)path;
    + (nullable instancetype)fileHandleForWritingAtPath:(NSString *)path;
    + (nullable instancetype)fileHandleForUpdatingAtPath:(NSString *)path;
    
    //读取文件内容
    - (NSData *)readDataToEndOfFile;
    - (NSData *)readDataOfLength:(NSUInteger)length;
    
    //写入内容
    - (void)writeData:(NSData *)data;
    
    //直接偏移文件的最后
    - (unsigned long long)seekToEndOfFile;
    
    //文件偏移
    - (void)seekToFileOffset:(unsigned long long)offset;
    
    //指定文件长度,多余的数据将会被清除
    - (void)truncateFileAtOffset:(unsigned long long)offset;
    
    //关闭文件
    - (void)closeFile;
    
    //应该是刷新同步数据使用的,不太确定。暂时没有使用
    - (void)synchronizeFile;
    

    栗子:

    //1 文件路径
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"test.txt"];
        //2 创建文件管理对象
        NSFileManager *manager = [NSFileManager defaultManager];
        
        //3创建文件
        NSString *str = @"sgdhajhdg";
        [manager createFileAtPath:path contents:[str dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
        NSLog(@"路径 = %@",path);
        
        //4 文件内容操作 创建文件对接器
        NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:path];
        
        //5 偏移插入位置
        [handle seekToFileOffset:3];
        
        NSData *resultData = [handle readDataToEndOfFile];
        NSString *str2 = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
        NSLog(@"str2  = %@",str2);
        
        [handle seekToFileOffset:3];
        //6写入数据
        NSString *str3 = [NSString stringWithFormat:@"你好哈%@",str2];
        [handle writeData:[str3 dataUsingEncoding:NSUTF8StringEncoding]];
        
        //7 关闭文件
        [handle closeFile];
    

    可能看到第5步往下你会有一点迷糊,为什么不直接

    [handle seekToFileOffset:3];
    

    走了这一步之后,追加数据。按照这个思路,最后得到的文件内容是

    ABC你好哈
    

    而不是

    ABC你好哈DEFG
    

    各位同学可以自己测试一下,看下结果。
    还有一点需要特别注意,

    //5 偏移插入位置
        [handle seekToFileOffset:3];
    

    这一个偏移量需要特别注意,不是随便写(我这是英文字符没有问题),如果是文件里面存储的中文字符的话,就可以出问题。你要知道,你通过偏移量得到的是NSData数据,NSString获取NSData的话,通常使用NSUTF8StringEncoding去解析。UTF-8一个中文字符占3个字节,那如果你偏移的不是3的倍数的话,解析的数据估计就会出问题。这个建议大家去看看UTF-8的知识点。
    Demo地址链接

    相关文章

      网友评论

        本文标题:iOS数据文件存储二

        本文链接:https://www.haomeiwen.com/subject/bswgjxtx.html