Home > Archive > C# > November 2004 > Replacing Text in Large Files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Replacing Text in Large Files
|
|
|
| I am using System.IO to read large text files (10GB). Each line in
these files is a record, and the main pupose of this application is to
validate the data. This I do by reading the file line by line and
looping thru the lines and all's fine n dandy. Until now.
Here's where the problem is - for certain records (lines) in a very
few of these files, I need to edit a part of the line. that is..
replace text in the middle for a few files.
The only way I could accomplish this so far was by using a separate
Intermediate file and writing to it. This obviously is not the most
optimized way to do it.
1. What would be ideal would be to edit text while reading the file.
2. If that cannot be done, I could Close the StreamReader and edit
text at that particular position (using StreamWriter.basestream.s
method ??)
Anyway, neither 1 nor 2 is working for me. Can anyone help please.
| |
| Joel Martinez 2004-11-16, 6:54 pm |
| What is it that's not working about #2?
A FileStream should be s able, and if you open it readwrite, you
should be able to pass it into the constructor of a StreamReader and
StreamWriter. So you read with the reader, and when you come across
something that need to replace, s back to the position and rewrite
it with the writer.
FileStream fs = new FileStream(fpath, FileMode.Open,
FileAccess.ReadWrite);
StreamReader reader = new StreamReader(fs);
StreamWriter writer = new StreamWriter(fs);
*disclaimer: I didn't compile that, nor prove my theory ;-) but it
sounds like it should work *
Hope that helps,
Joel Martinez
Orlando .NET User Group
http://www.onetug.org
http://www.codecube.net
samittaneja@yahoo.com (sam) wrote in message news:<f2fe0738.0411151319.6c5ca09a@posting.google.com>...
> I am using System.IO to read large text files (10GB). Each line in
> these files is a record, and the main pupose of this application is to
> validate the data. This I do by reading the file line by line and
> looping thru the lines and all's fine n dandy. Until now.
>
> Here's where the problem is - for certain records (lines) in a very
> few of these files, I need to edit a part of the line. that is..
> replace text in the middle for a few files.
>
> The only way I could accomplish this so far was by using a separate
> Intermediate file and writing to it. This obviously is not the most
> optimized way to do it.
>
> 1. What would be ideal would be to edit text while reading the file.
>
> 2. If that cannot be done, I could Close the StreamReader and edit
> text at that particular position (using StreamWriter.basestream.s
> method ??)
>
> Anyway, neither 1 nor 2 is working for me. Can anyone help please.
|
|
|
|
|