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];
}
posted @ 2022-02-25 08:58  观海云不远  阅读(57)  评论(0编辑  收藏  举报