dudeSreeni Posted December 30, 2008 Posted December 30, 2008 (edited) Hi, I need to read a 4GB log file to extract some information and write it to another file. Can any one suggest me an ideal class i should use to read the file using C#. I'm planning to use Regular Expressions to match the lines that contain the parameter strings. currently the file is read and written using commands in DOS and it takes around 15 - 18 mins. I'm planning to make it much quicker with the application i'm planning to develop. My development machine has 2 GB of RAM. Can any one advise me on this. Thanks and Regards, Sree Edited December 30, 2008 by dudeSreeni Quote
Administrators PlausiblyDamp Posted December 30, 2008 Administrators Posted December 30, 2008 Could you not simply use a StreamReader and read the file a line at a time and check each line as you read it? If performance suffers a lnie at a time then you might find a FileStream will allow you to read chunks into memory and then use a stream reader over the memory you have just read in. Another possibility is using memory mapped files, however this isn't as straight forward from .Net although google will find several hits that will point you in the right direction. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted December 30, 2008 Posted December 30, 2008 As PD suggests, you might try something like this: StreamReader reader = new StreamReader(@"C:\your4GBFile.log",...more parms); while ((String line = reader.ReadLine()) != null) System.WriteLine(line); reader.Close(); Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Nate Bross Posted December 30, 2008 Posted December 30, 2008 Another thing worth keeping track of is string concatenation. While you are building up your second string in memory, that you will write out at the end, be sure to use the StringBuilder class. ...code... StringBuilder sb = new StringBuilder(); sb.Append(line); ...code... This code above is MUCH faster than this ...code... String s = ""; s += line; ...code... Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.