c# - Read the large text files into chunks line by line -


suppose following lines in text file have read

info  2014-03-31 00:26:57,829 332024549ms service1 startmethod - fillpropertycolor end info  2014-03-31 00:26:57,829 332024549ms service1 getreports_dataset - getreports_dataset started info  2014-03-31 00:26:57,829 332024549ms service1 cheduledgeneration - switchscheduledgeneration start info  2014-03-31 00:26:57,829 332024549ms service1 cheduledgeneration - switchscheduledgeneration limitid, subscriptionid, limitperiod, dtnextscheduleddate,shoplimittype0, 0, , 3/31/2014 12:26:57 am,0 

i use filestream method read text file because text file size having size on 1 gb. have read files chunks in first run of program read 2 lines i.e. "getreports_dataset started of second line". in next run should read 3rd line. did code unable desired output.problem code doesn't give exact chunk have start read text in next run. , second problem while reading text lines .. don't give complete line..i.e. part missing in lines. following code:

readposition = getlastreadposition(); using (filestream fstream = new filestream(logfilepath, filemode.open, fileaccess.read, fileshare.readwrite)) using (system.io.streamreader rdr = new system.io.streamreader(fstream)) {     rdr.basestream.seek(readposition, seekorigin.begin);     while (numcharcount > 0)     {         int numchars = rdr.readblock(block, 0, block.length);          string blockstring = new string(block);         lines = blockstring.split(convert.tochar('\r'));         lines[0] = fragment + lines[0];         fragment = lines[lines.length - 1];          foreach (string line in lines)         {             lsttextlog.add(line);             if (lsttextlog.contains(fragment))             {                 lsttextlog.remove(fragment);             }             numprocessedchar++;         }         numcharcount--;     }     setlastposition(numprocessedchar, logfilepath); } 

if want read file line-by-line, this:

foreach (string line in file.readlines("filename")) {     // process line here } 

if must read line , save position, need save last line number read, rather stream position. example:

int lastlineread = getlastlineread(); string nextline = file.readlines("filename").skip(lastlineread).firstordefault(); if (nextline != null) {     lastlineread++;     setlastposition(lastlineread, logfilepath); } 

the reason can't saving base stream position because streamreader reads large buffer full of data base stream, moves file pointer forward buffer size. streamreader satisfies read requests buffer until has read next buffer full. example, open streamreader , ask single character. assuming has buffer size of 4 kilobytes, streamreader this:

if (buffer empty) {     read buffer (4,096 bytes) base stream     buffer_position = 0; } char c = buffer[buffer_position]; buffer_position++;    // increment position next read return c; 

now, if ask base stream's position, it's going report position @ 4096, though you've read 1 character streamreader.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -