Read file contents operation-(void) loadfilecontentsintotextview{ //Open a file via stream nsinputstream *inputstream = [[Nsinputstream alloc] Initwithfileatpath:filepath]; [InputStream Open]; Nsinteger maxLength = 128;uint8_t Readbuffer [maxlength];//has been to the end to identify bool endofstreamreached = no;//Note:this tight loop would block until Stream Endswhile (! endofstreamreached) {Nsinteger bytesread = [InputStream read:readbuffer Maxlength:max Length];if (Bytesread = = 0) {//file read to last endofstreamreached = YES;} else if (bytesread = =-1) {//file read error endofstreamreached = YES;} Else{nsstring *readbufferstring =[[nsstring alloc] initwithbytesnocopy:readbuffer length:bytesread encoding: Nsutf8stringencoding Freewhendone:no];
The asynchronous file reads, in the network aspect, because the network's unreliability may cause the Nsfilemanager file operation method the blocking, but the flow operation can realize the asynchronous reading. Nsstream can work asynchronously. You can register a callback function that has a byte-readable time in the stream, and if it is not readable, do not block the callback.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS reads large files