How to read a multiline string from a file using StreamReader?

133 Views Asked by At

Given this text data from a file:

Mehmet;Demirayak;IT
1
Emre;Araz;Sekten Chef
1
Nusret;Beganovic;Kein Geld
2
Ferhat;Acer;Acer Computer
2
Fevzi;Uzun;Trabzon

my goal is to read it using a StreamReader and store it in my defined class Personen.

If I had to split only one argument, it's not a problem. In other words, if the text in the file would be written only with the separator ";" and without any space or new line characters, I'd know how to read it. However, when I get a more complex structure in my data, such the one above, I am not sure how to read it.

This is why I have tried so far:

public void read()
{
   StreamReader sr = new StreamReader(datei);
   string s;
   int i = 0;

    while ((s = sr.ReadLine()) != null)
     {
          string[] l = s.Split(";");
      if(s.StartsWith("1") || s.StartsWith("2"))
      {
          p[i] = new Personen(l[0], l[1], l[2]);
          Console.WriteLine(p[i]);
          i++;
      }
   }

}
1

There are 1 best solutions below

7
jarmanso7 On

Based on the discussion in the comments under your question, in order to achieve your goal you can use the following logic for each line you read. If you indeed read '1' or '2', then skip the split operation for the current line and jump one iteration ahead by using continue (thanks @PeterB for pointing out my mistake in the original answer). If not, then proceed to split the current line and store it into p:

public static void Read(Stream stream)
{
    using StreamReader sr = new StreamReader(stream);
    string s;
    int i = 0;

    while ((s = sr.ReadLine()) != null)
    {
        var lineStartsWithControlDigit = s[0] == '1' || s[0] == '2';

        if (lineStartsWithControlDigit)
        {
            continue;
        }

        string[] l = s.Split(";");
        p[i] = new Personen(l[0], l[1], l[2]);
        
        Console.WriteLine(p[i]);
        
        i++;
    }
}

Also note that your statement Console.WriteLine(p[i]) is going to print only "Personen" which is not so meaningful. In order for it to print nicely the contents of the Personen object in p[i], you can override the method ToString() in class Personen:

public class Personen
{
    string FirstPerson { get; set; }
    string SecondPerson { get; set; }
    string ThirdPerson { get; set; }

    public Personen(string firstPerson, string secondPerson, string thirdPerson)
    {
        FirstPerson = firstPerson;
        SecondPerson = secondPerson;
        ThirdPerson = thirdPerson;
    }

    public override string ToString()
    {
        return $"{FirstPerson};{SecondPerson};{ThirdPerson}";
    }
}

Running the program and calling read() produces the following output in the console:

Mehmet;Demirayak;IT
Emre;Araz;Sekten Chef
Nusret;Beganovic;Kein Geld
Ferhat;Acer;Acer Computer
Fevzi;Uzun;Trabzon

It's important to close the stream once you are done working with it, that's why I also added using in front of the instantiation of StreamReader sr. See more about this at Do I need to explicitly close the StreamReader in C# when using it to load a file into a string variable?.