/** 此处使用NSCachesDirectory App中的缓存文件夹作为案例 */
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [pathArray firstObject];
//在cache文件夹下新建一个GTData文件夹
NSString *dataPath = [cachePath stringByAppendingPathComponent:@"GTData"];
NSFileManager *fileManager = [NSFileManager defaultManager];
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) {
[fileManager removeItemAtPath:listDataPath error:nil];
}
NSFileHandle *fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:listDataPath];
[fileHandler seekToEndOfFile];
[fileHandler writeData:[@"def" dataUsingEncoding:NSUTF8StringEncoding]];
//刷新文件中的数据
[fileHandler synchronizeFile];
//关闭文件操作(若不主动关闭,在销毁FileHandle时系统也会自动关闭)
[fileHandler closeFile];