Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the BEST way to replace text in a File using C# / .NET?

I have a text file that is being written to as part of a very large data extract. The first line of the text file is the number of "accounts" extracted.

Because of the nature of this extract, that number is not known until the very end of the process, but the file can be large (a few hundred megs).

What is the BEST way in C# / .NET to open a file (in this case a simple text file), and replace the data that is in the first "line" of text?

IMPORTANT NOTE: - I do not need to replace a "fixed amount of bytes" - that would be easy. The problem here is that the data that needs to be inserted at the top of the file is variable.

IMPORTANT NOTE 2: - A few people have asked about / mentioned simply keeping the data in memory and then replacing it... however that's completely out of the question. The reason why this process is being updated is because of the fact that sometimes it crashes when loading a few gigs into memory.

like image 234
Timothy Khouri Avatar asked May 13 '09 16:05

Timothy Khouri


2 Answers

If you can you should insert a placeholder which you overwrite at the end with the actual number and spaces.

If that is not an option write your data to a cache file first. When you know the actual number create the output file and append the data from the cache.

like image 104
laktak Avatar answered Sep 28 '22 02:09

laktak


BEST is very subjective. For any smallish file, you can easily open the entire file in memory and replace what you want using a string replace and then re-write the file.

Even for largish files, it would not be that hard to load into memory. In the days of multi-gigs of memory, I would consider hundreds of megabytes to still be easily done in memory.

Have you tested this naive approach? Have you seen a real issue with it?

If this is a really large file (gigabytes in size), I would consider writing all of the data first to a temp file and then write the correct file with the header line going in first and then appending the rest of the data. Since it is only text, I would probably just shell out to DOS:

 TYPE temp.txt >> outfile.txt
like image 42
Jack Bolding Avatar answered Sep 28 '22 02:09

Jack Bolding