Swapna

Swapna

  • NA
  • 21
  • 3.4k

Read specific data from text file

Oct 2 2012 12:38 AM
 I have a text file as follows(it is having more than 1 lakh lines):
    Header
    AGROUP1
    ADATA1|0000
    ADATA2|0001
    ADATA3|0002
    D0000|TNE
    D0001|TNE
    D0002|TNE
    AGROUP2
    ADATA1|0000
    ADATA2|0001
    ADATA3|0002
    D0000|TNE
    D0001|TNE
    D0002|TNE
    AGROUP3
    ADATA1|0000
    ADATA2|0001
    ADATA3|0002
    D0000|TNE
    D0001|TNE
    D0002|TNE
Infact it has more than 1 lakh lines of code.
I need to read data based on group
For example in a method:
    public void ReadData(string strGroup)
    {
        if(strGroup == "AGROUP2)
           //Read from the text file starting from line  "AGROUP2" to "AGROUP3"(i.e lines under AGROUP2)
    }
What i have tried is
  
     public void ReadData(string strGroup)
        {
                 bool start = false;
                 while ((line = reader.ReadLine()) != null)
            {
             if (line == strGroup && line.Length == 5)
              start = true;
             else if (line.Length == 5)
              start = false;
             if(start)
              yield return line;
            }
        }
It is working fine, Performance wise, it takes longer since my text file is a very very huge file....There is if condition on every line in the method.
IS the a better way to do this?

Answers (2)