-(NSString*)dateFilePath{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFileName];开发者_JS百科
}
- (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];
There in a file in Documents,I want to load into buffer use this method,but actuallyRead always -1, is there some wrong in my method?
The reason you always get -1, is that you forgot to open the stream, so all you have to do is insert the line [inStream open];
after you initialized your NSInputStream and before you try to read from it.
You should check if the inStream is available by
if (nil == inStream)
精彩评论