edit a text file

  • Thread starter Thread starter Abhishek
  • Start date Start date
A

Abhishek

Hi!

I need to edit a text file.
I need to replace a given text in the file with a new one.
This text can exist anywhere in the file, so i first need to locate the text
position and then edit it.
How do i acheive this.



Abhishek
 
Hi Abhishek,

Read the entire file into a string, edit the string, and write it all back
to the file.
 
hi,

depending of the size of the file you can do two things:

1- if the file is small read it completely in a string and use
String.Replace , write the file back
2- if file is big , read it line by line, replace string and write to a temp
file, at the end overwrite the original file

cheers,
 
I> 2- if file is big , read it line by line, replace string and write to
I> a temp
I> file, at the end overwrite the original file

If the file is big, you cannot read it line by line, just because there may
be only one line of text, all that big file, and you'll either gain an OutOfMemoryException
or degrade the performance dramatically. Should do processing in a streaming
fashion with a lookahead of at least the search term in size if you expect
large sizes.
 
Back
Top