其他分享
首页 > 其他分享> > iOS开发学习笔记(OC语言)——文件基本操作

iOS开发学习笔记(OC语言)——文件基本操作

作者:互联网

文件基本操作

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths firstObject];

NSFileManager *fileManager = [NSFileManager defaultManager];

//创建文件夹
NSString *dataPath = [cachePath stringByAppendingPathComponent:@"FirstData"];
NSError *createError;
[fileManager createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&createError];

//创建文件
NSString *listDataPath = [dataPath stringByAppendingPathComponent:@"list"];
NSData *listData = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];
[fileManager createFileAtPath:listDataPath contents:listData attributes:nil];

//查询文件
BOOL fileExist = [fileManager fileExistsAtPath:listDataPath];

if (fileExist) {
    //文件追加内容
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:listDataPath];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:[@"\ndef" dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandle synchronizeFile];
    [fileHandle closeFile];
}

标签:文件,fileManager,iOS,dataPath,C语言,listDataPath,NSString,基本操作,fileHandle
来源: https://www.cnblogs.com/lurenjiashuo/p/15934438.html