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:
- Read each line from the original file by using
StreamReader
. - Open a
StringBuilder
for the contents of the new file. - Modify the string object and call
AppendLine
of theStringBuilder
(until the end of the file) - Open a new
StreamWriter
, and write theStringBuilder
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.