I'm looking to load a 150 MB text file into a string. The file is UTF16 encoded, so it will produce a string that's about 150 MB in memory. All the methods I have tried result in an Out of Memory exception.
I know this is a huge string, and certainly not how I'd like to do things. But there's really not much I can do about that at the moment without a lot of really deep changes to an application about to head out the door. The file does not have an evenly distributed set of lines in it. One line can contain 80% or so of the entire file size.
Here's what I've tried:
Method 1
// Both of these throw Out of Memory exception
var s = File.ReadAllText(path)
var s = File.ReadAllText(path, Encoding.Unicode);
Method 2
var sb = new StringBuilder();
// I've also tried a few other iterations on this with other types of streams
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
string line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
// This throws an exception
sb.ToString();
Method 3
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sr = new StreamReader(fs, Encoding.Unicode))
{
int initialSize = (int)fs.Length / 2; // Comes to a value of 73285158 with my test file
var sb = new StringBuilder(initialSize); // This throws an exception
string line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
sb.ToString();
}
So, what can I do to load this file into a string variable?
Edit: Added additional attempts to resolve issue based on comments.