Linux教程網
最近要在模擬器環境下收集一些數據日志。
然後考慮到數據記載和追加功能。
簡單代碼如下
- +(void) appString:(NSString *) s
- {
- BOOL isExit = [NLCommonUtil isExitOfFile:@"nonotextiphone"];
- NSString *filePath = [self FilePathInDocumentByFileName:@"nonotextiphone"];
- if (!isExit) {
- NSLog(@"%@",@"文件不存在");
- NSString *s = [NSString stringWithFormat:@"開始了:\r"];
- [s writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- }
- NSFileHandle *outFile;
- NSData *buffer;
- outFile = [NSFileHandle fileHandleForWritingAtPath:filePath];
- if(outFile == nil)
- {
- NSLog(@"Open of file for writing failed");
- }
- //找到並定位到outFile的末尾位置(在此後追加文件)
- [outFile seekToEndOfFile];
- //讀取inFile並且將其內容寫到outFile中
- NSString *bs = [NSString stringWithFormat:@"%@",s];
- buffer = [bs dataUsingEncoding:NSUTF8StringEncoding];
- [outFile writeData:buffer];
- //關閉讀寫文件
- [outFile closeFile];
- }
Copyright ©
Linux教程網 All Rights Reserved