Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can File.AppendAllText be used with very large files without causing memory issues?

Tags:

.net

file-io

I need to write to a very large text file by adding content to it as I interate over a very large set of records in c# 3.5. This file could be several GBs in size. I thought that I would use File.AppendAllText to write to the file after each record is processed. That way I don't keep the file in memory. However, will the AppendAllText read the entire contents of the file into memory when called or will it just add the new content to the end of it?

like image 919
user31673 Avatar asked Sep 05 '25 02:09

user31673


2 Answers

Apppending doesn't mean it loads the existing content, so yes you can use AppendAllText for this. Note, however, that a StreamWriter could also be used, perhaps more cleanly, to write gradually to a file (plus without having to keep open/close the file.

like image 197
Marc Gravell Avatar answered Sep 07 '25 14:09

Marc Gravell


From the documentation for File.AppendAllText it looks like it does not read the file into memory.

Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

From http://msdn.microsoft.com/en-us/library/ms143356.aspx

like image 40
Will Warren Avatar answered Sep 07 '25 16:09

Will Warren