使用NSFileHandle进行文件读/写的示例 [英] Example for file read/write with NSFileHandle
本文介绍了使用NSFileHandle进行文件读/写的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- (void)performFileOperation
{
@try {
NSFileHandle * inputFileHandle;
NSFileHandle * outputFileHandle;
//检查输出文件是否存在,如果没有创建新文件。
NSFileManager * morphedFileManager;
outputFileManager = [NSFileManager defaultManager];
if([outputFileManager fileExistsAtPath:self.outputFilePath] == NO)
{
NSLog(@Output file does not exist,creating a new one);
[outputFileManager createFileAtPath:self.outputFilePath
contents:nil
attributes:nil];
}
NSData * inputDataBuffer;
inputFileHandle = [NSFileHandle fileHandleForReadingAtPath:self.inputFilePath];
NSAssert(inputFileHandle!= nil,@无法打开输入文件的句柄);
outputFileHandle = [NSFileHandle fileHandleForReadingAtPath:self.outputFilePath];
NSAssert(outputFileHandle!= nil,@无法打开输出文件的句柄);
@try {
//寻找文件的开头
[inputFileHandle seekToFileOffset:0];
[outputFileHandle seekToFileOffset:0]; ((inputDataBuffer = [inputFileHandle readDataOfLength:1024])!= nil)
$ {
[outputFileHandle writeData:[self.fileWrapper process:inputDataBuffer]];
@catch(NSException * exception){
@throw;
}
@finally {
[inputFileHandle closeFile];
[outputFileHandle closeFile];
@catch(NSException * exception){
@throw;
$ b 在尝试写入时出现以下异常:
$ b $ / b>
无法处理输入缓冲区(*** - [NSConcreteFileHandle writeData:]:错误的文件描述符)
$
解决方案需要更改 outputFileHandle
line :
outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:self.outputFilePath];
Are there good examples for doing file read/write in chunks with objective c? I am new to objective-c and iPhone API, here is the sample code that I wrote. Are there better ways to do it?
-(void) performFileOperation
{
@try {
NSFileHandle *inputFileHandle;
NSFileHandle *outputFileHandle;
//check whether the output file exist, if not create a new one.
NSFileManager *morphedFileManager;
outputFileManager = [NSFileManager defaultManager];
if ([outputFileManager fileExistsAtPath: self.outputFilePath ] == NO)
{
NSLog (@"Output file does not exist, creating a new one");
[outputFileManager createFileAtPath: self.outputFilePath
contents: nil
attributes: nil];
}
NSData *inputDataBuffer;
inputFileHandle = [NSFileHandle fileHandleForReadingAtPath: self.inputFilePath];
NSAssert( inputFileHandle != nil, @"Failed to open handle for input file" );
outputFileHandle = [NSFileHandle fileHandleForReadingAtPath: self.outputFilePath];
NSAssert( outputFileHandle != nil, @"Failed to open handle for output file" );
@try{
// seek to the start of the file
[inputFileHandle seekToFileOffset: 0];
[outputFileHandle seekToFileOffset: 0];
while( (inputDataBuffer = [inputFileHandle readDataOfLength: 1024]) != nil )
{
[outputFileHandle writeData: [self.fileWrapper process: inputDataBuffer]];
}
}
@catch (NSException *exception) {
@throw;
}
@finally {
[inputFileHandle closeFile];
[outputFileHandle closeFile];
}
}
@catch (NSException *exception) {
@throw;
}
}
I get the following exception while trying to write:
Failed to process input buffer ( *** -[NSConcreteFileHandle writeData:]: Bad file descriptor )
解决方案 Needs to change outputFileHandle
line:
outputFileHandle = [NSFileHandle fileHandleForWritingAtPath: self.outputFilePath];
这篇关于使用NSFileHandle进行文件读/写的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文