Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by dudeSreeni
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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();

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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...

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...