Ios reads large files
// Read File Content operation-(void) loadFileContentsIntoTextView {// open a file through the stream NSInputStream * inputStream = [[NSInputStream alloc] initWithFileAtPath: filePath]; [inputStream open]; NSInteger maxLength = 128; uint8_t readBuffer [maxLength]; // whether BOOL endOfStreamReached = NO has been identified by the end; // NOTE: this tight loop will block until stream endswhile (! EndOfStreamReached) {NSInteger bytesRead = [inputStream read: readBuffer maxLength: maxLength]; if (bytesRead = 0) {// The file is read to the end of endOfStreamReached = YES ;} else if (bytesRead =-1) {// file read error endOfStreamReached = YES;} else {NSString * readBufferString = [[NSString alloc] initWithBytesNoCopy: readBuffer length: bytesRead encoding: NSUTF8StringEncoding freeWhenDone: NO]; // continuously loads characters to the view [self appendTextToView: readBufferString]; [readBufferString release] ;}} [inputStream close]; [inputStream release];}
Asynchronous File Reading: in terms of the network, the network reliability may cause the NSFileManager file operation method to be blocked, while the stream-based operations can achieve asynchronous reading. NSStream can work asynchronously. You can register a function for callback when the stream contains bytes that are readable. If there is no readable function, do not block it and call back it out.