Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to write huge string into a file

In C#, I'm reading a moderate size of file (100 KB ~ 1 MB), modifying some parts of the content, and finally writing to a different file. All contents are text. Modification is done as string objects and string operations. My current approach is:

  1. Read each line from the original file by using StreamReader.
  2. Open a StringBuilder for the contents of the new file.
  3. Modify the string object and call AppendLine of the StringBuilder (until the end of the file)
  4. Open a new StreamWriter, and write the StringBuilder to the write stream.

However, I've found that StremWriter.Write truncates 32768 bytes (2^16), but the length of StringBuilder is greater than that. I could write a simple loop to guarantee entire string to a file. But, I'm wondering what would be the most efficient way in C# for doing this task?

To summarize, I'd like to modify only some parts of a text file and write to a different file. But, the text file size could be larger than 32768 bytes.

== Answer == I'm sorry to make confusin to you! It was just I didn't call flush. StremWriter.Write does not have a short (e.g., 2^16) limitation.

like image 546
Nullptr Avatar asked Feb 17 '11 09:02

Nullptr


2 Answers

StreamWriter.Write

does not

truncate the string and has no limitation.

Internally it uses String.CopyTo which on the other hand uses unsafe code (using fixed) to copy chars so it is the most efficient.

like image 116
Aliostad Avatar answered Oct 13 '22 12:10

Aliostad


The problem is most likely related to not closing the writer. See http://msdn.microsoft.com/en-us/library/system.io.streamwriter.flush.aspx.

But I would suggest not loading the whole file in memory if that can be avoided.

like image 26
Goran Avatar answered Oct 13 '22 13:10

Goran