5

I'm trying to read in a 150mb text file into a Rich Text box.

Currently, I am using a StreamReader to iterate through each line in the file, appending every line to a StringBuilder instance.

This works for smaller files, but I get a System.OutOfMemory exception when trying to read large files.

I don't see any problems with reading a 150mb file - there is plenty of physical memory and that's well within the Windows 32-bit application address space.

If anyone here has any idea how to do this, It would be greatly appreciated.

I'll attach my code at the end.

Thanks.

StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(fileLocation))
{
   string line;
   while ((line = sr.ReadLine()) != null)
   {
      sb.AppendLine(line);
   }
    }

return sb;
  • 1
    Could you post a stack trace? So we can see exactly where the exception occurs. Also, this might be related: http://stackoverflow.com/questions/363680/stringbuilder-for-string-concatenation-throws-outofmemoryexception (regarding string builders) – Christoffer Feb 29 '12 at 11:53
  • 1
    Please clarify, does this code (the reading part) throw the exception or does that happen when you load the RTB ? – H H Feb 29 '12 at 11:54
  • But 150MB text file ? Does it contain all the books written in 19th century ? :) – abhilash Feb 29 '12 at 11:56
  • Initialize with an initial size, decreases the need for resizing. Remember it doubles the buffer size every time it reaches its max. The size can be set from the constructor. Follow this by calling .EnsureCapacity() to make room in memory. – Silas Hansen Feb 29 '12 at 12:04
  • Has anyone even asked WHY? What on earth do you intend to do with text information containing 150 million characters? Even if you managed to load it, I doubt you could edit it (or even scroll it) without more issues. Perhaps you could post what it is you're trying to achieve? – adelphus Mar 01 '12 at 11:47

2 Answers2

5

Use RichTextBox.LoadFile

http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.loadfile.aspx

I'm not sure why you would want to load the entire text to a StringBuilder. Alternatively you could pass a FileStream to LoadFile which would render the large file for you.

abhilash
  • 5,605
  • 3
  • 36
  • 59
0

I guess you should manage somehow the input file - let say split it into several less files and navigate between the parts programmatically or so..

150MB file sounds like an abnormal thing. Maybe you should look at the stream kind of data processing rather than file one.

Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121