0

I am using C#.net

How can read a text file block by block, the block is separated by new line character.

Block size is not fixed, so i am not able to use ReadBlock method of StreamReader.

Is there any ohter method for getting the data block by block as it is separated by new line character.

usr021986
  • 3,421
  • 14
  • 53
  • 64

4 Answers4

3

You could use a StreamReader:

using (var reader = File.OpenText("foo.txt"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        // do something with the line
    }
}

This method reads the text file line by line (where Environment.NewLine is used as line separator) and has only the current line loaded in memory at once so it could be used to read very large files.

If you just want to load all the lines of a small text file in memory you could also use the ReadAllLines method:

string[] lines = File.ReadAllLines("foo.txt");
// the lines array will contain all the lines
// don't use this method with large files
// as it loads the entire contents into memory
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

Something like:

using (TextReader tr = new StreamReader(FullFilePath))
{
  string Line;
  while ((Line = tr.ReadLine()) != null)
  {
    Line = Line.Trim();
  }
}

Funny I was doing some file i/o the other day, and found this which was very useful at processing large delimited record text files (e.g. converting a record to a pre-defined type which you define yourself)

Jeb
  • 3,689
  • 5
  • 28
  • 45
0

You can look at StreamReader.ReadToEnd() and String.Split()

use:

string content = stream.ReadToEnd();
string[] blocks = content.Split('\n');//You may want to use "\r\n"
annonymously
  • 4,708
  • 6
  • 33
  • 47
  • 1
    Why going through all this pain when there is a single method that allows you to do that? – Darin Dimitrov Jan 18 '12 at 12:57
  • Oh, your right. I hadn't noticed that before. Well, useless answer. Although, My method could split the file up into blocks separated by any delimiter. – annonymously Jan 18 '12 at 12:58
0

You could use the File.ReadLines method

foreach(string line in File.ReadLines(path))
{
   //do something with line
}

ReadLines returns an IEnumerable<string> so only one line is stored in memory at once.

Ray
  • 45,695
  • 27
  • 126
  • 169