c# - How to edit a file, appending data at the beginning, without rewriting the whole file? -


i have csv file, , need add unique id based on first 2 characters of file. have following code:

using (streamreader sr = new streamreader(f))                     {                         string currentline;                         int id = 0;                          while ((currentline = sr.readline()) != null)                         {                             string row = currentline.tostring();                             string firsttwocharacters = currentline.substring(0, 2);                               if (firsttwocharacters == "01")                             {                                 id = id + 1;                                 row += "*" + id.tostring();                                  using (streamwriter files = file.appendtext(dir + newfilename))                                 {                                     files.writeline(row);                                 }                             }                             else                             {                                 row += "*" + id.tostring();                                  using (streamwriter files = file.appendtext(dir + newfilename))                                 {                                     files.writeline(row);                                 }                             }                          }                     } 

the csv files can huge, 1gb in size, around 6 million rows. wanted advice, if there quicker way handling this, can take 3+ hours process file, , multiple files can received in 1 go.

opening (file.appendtext) inside while loop costly, move outside while

using (streamreader sr = new streamreader(f)) {                 string currentline;                 int id = 0;                 using (streamwriter files = file.appendtext(dir + newfilename))                 {                     while ((currentline = sr.readline()) != null)                     {                         string row = currentline.tostring();                         string firsttwocharacters = currentline.substring(0, 2);                           if (firsttwocharacters == "01")                         {                             id = id + 1;                             row += "*" + id.tostring();                              files.writeline(row);                          }                         else                         {                             row += "*" + id.tostring();                              files.writeline(row);                          }                      }                 }             } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -