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];
}
上一篇:每天一个linux命令(15):tail 命令


下一篇:Xcode Debugger Extremely Slow , Xcode Swift调试器调试时极其慢(