NSString *savepath = [self saveFilePath:@"savepath"];
NSString *outpath = [self saveFilePath:@"outpath"];
NSString *text = @"This is test text in file.";
NSData *buffer;
if (![fm fileExistsAtPath:savepath]) {
[fm createFileAtPath:savepath contents:nil attributes:nil];
}
if (![fm fileExistsAtPath:outpath]) {
[fm createFileAtPath:outpath contents:nil attributes:nil];
}
NSMutableData *data = [[NSMutableData alloc]init];
[data appendData:[text dataUsingEncoding:NSUTF8StringEncoding]];
[data writeToFile:savepath atomically:YES];
NSFileHandle *inFile, *outFile;
inFile = [NSFileHandle fileHandleForReadingAtPath:savepath];
outFile = [NSFileHandle fileHandleForWritingAtPath:outpath];
if (outFile != nil) {
[outFile truncateFileAtOffset:0];
buffer = [inFile readDataToEndOfFile];
[outFile writeData:buffer];
[inFile closeFile];
[outFile closeFile];
}
NSLog(@"%@", [NSString stringWithContentsOfFile:outpath encoding:NSUTF8StringEncoding error:nil]);
NSFileHandle简单的读写文件
最新推荐文章于 2018-03-22 00:37:50 发布