0

I want to populate the arraylist with the contents of the file. now it reads the first line and adds it to 0 and reads the next line and again writes it to 0, so basically it keeps overwriting itself. how can i make it add to the next avail element?

        ArrayList files = new ArrayList();
        StreamReader sr = new StreamReader("C:\\TestFile.txt");
        String line;
        while ((line = sr.ReadLine()) != null)
        {

            files.Add(sr.ReadLine());
          for (int i=0; i<files.Count; i++)
            {
                files.Add(sr.ReadLine());
            }

        }
        sr.Close();

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by dickersonka
0
ArrayList files = new ArrayList();
StreamReader sr = new StreamReader("C:\\TestFile.txt");
String line;
while ((line = sr.ReadLine()) != null)
{
files.Add(line);
}
sr.Close();

you don't need that for loop in there, and a call to readline makes the stream read again, you are using line as sr.ReadLine() so you needed to add it

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.