user9379281
user9379281

Reputation:

how to work with big text in c#?

I am editing some lists that I wrote before with if condition inside a loop but if the text file is more than about 20 MB it says overflow. and if more than 1 MB it loads for ever. For example

string[] x = File.ReadAllLines(openFileDialog1.FileName);

string a = "";

for (int i = 0; i < x.Length; i++)
{
    if (x[i].Length > 9 && x[i] < 13)
    {
        a += "" + x[i] + "\r\n";
    }
}

that is just an example if you know a topic that could help me please post it

Upvotes: 0

Views: 128

Answers (2)

lazydeveloper
lazydeveloper

Reputation: 961

you should use BufferedStream object to read/write larger file and it will also improve execution time.

   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)
    {
       //write your logic here
    }
   }

Update : go through link for fastest way to read file Fastest way to read file

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

Start from changing String to StringBuilder:

string[] x = File.ReadAllLines(openFileDialog1.FileName);

StringBuilder sb = new StringBuilder();

//TODO: x[i] < 13 (taken from the question) seems to be erroneous
// Do you mean x[i].Length < 13?
for (int i = 0; i < x.Length; i++)
  if (x[i].Length > 9 && x[i] < 13) 
    sb.AppendLine(x[i]);

string a = sb.ToString();

Next attempt can be Linq, something like this:

string a = string.Join(Environment.NewLine, File
  .ReadLines(openFileDialog1.FileName)
  .Where(line => line.Length > 9 && line.Length < 13));

When executing a += you recreate a string (string is immutable class and can't be modified); recreating string in a loop can well appear to be deadly slow. StringBuilder is the class specially designed for such cases.

Upvotes: 3

Related Questions