关于NSInputstream

时间:2011-05-25 05:57:15

标签: nsinputstream

-(NSString*)dateFilePath{

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory=[paths objectAtIndex:0];

return [documentsDirectory stringByAppendingPathComponent:kFileName];
}


- (void)viewDidLoad
{

int actuallyRead=0;

NSString *path=[self dateFilePath];

NSURL *audiourl=[[NSURL alloc]initFileURLWithPath:path];

NSLog(@"%@",audiourl);

inStream=[[NSInputStream alloc]initWithURL:audiourl];

actuallyRead=[inStream read:buffer maxLength:sizeof(buffer)];

NSLog(@"%d",actuallyRead);

[dataBuffer1 appendBytes:buffer length:actuallyRead];

NSLog(@"%d",actuallyRead);

[inStream release];

[super viewDidLoad];

}
IN .h file
NSInputStream  *inStream;
NSMutableData  *dateBuffer1;
unint8_t       buffer[1024];

在Documents中的一个文件中,我想加载到缓冲区使用这个方法,但实际上读取总是-1,我的方法中有错误吗?

2 个答案:

答案 0 :(得分:2)

你总是得到-1的原因是你忘了打开流,所以你要做的就是在初始化NSInputStream之后和尝试读取之前插入行[inStream open];。 / p>

答案 1 :(得分:0)

您应该通过

检查inStream是否可用
if (nil == inStream)