0
点赞
收藏
分享

微信扫一扫

iOS文件管理

静鸡鸡的JC 2021-09-19 阅读 39
iOS




- (void)_getSandBoxPath {

    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    NSString*cachesPath = [pathArray firstObject];

    NSFileManager *fileManger = [NSFileManager defaultManager];


    //创建文件夹

    NSString *dataPath = [cachesPath stringByAppendingPathComponent:@"HomeListData"];

    NSError *createError;

    [fileManger createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&createError];


    //创建文件

    NSString *listDataPath = [dataPath stringByAppendingPathComponent:@"listData"];

    NSData *listData = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];

    [fileManger createFileAtPath:listDataPath contents:listData attributes:nil];


    //查询文件

    __unused BOOL isExist = [fileManger fileExistsAtPath:listDataPath];


//    if (isExist) {

//        [fileManger removeItemAtPath:listDataPath error:nil];

//    }


    //更新文件

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:listDataPath];

    [fileHandle seekToEndOfFile];

    [fileHandle writeData:[@"def" dataUsingEncoding:NSUTF8StringEncoding] error:nil];

    [fileHandle synchronizeFile];

    [fileHandle closeFile];

}

举报

相关推荐

0 条评论