3

I have a method that reads a text file and writes the contents to an array list. The array list is declared globally with no elements. This method works perfectly.

private void LoadArrayList()
{
    try
    {
        string actor;
        TextReader tr;
        tr = File.OpenText("actors.txt");

        while ((actor = tr.ReadLine()) != null)
        {
            ActorArrayList.Add(actor);
        }
        tr.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error loading file!");
    }
}

I now need to create a method that reads the array list and writes the contents back to the text file, replacing what is currently in the text file. This method results in a blank text file. Any idea's what I am missing/doing wrong?

private void WriteArrayList()
{
    TextWriter tw;

    try
    {
        tw = File.CreateText("actors.txt");
        foreach (object o in ActorArrayList)
            tw.WriteLine(o.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error saving file!");
    }
}

NOTE: I know people don't seem to like array lists or TextReader or TextWriter, but the course I am doing covers them so I am required to use them.

5
  • Any particular reason you are using ArrayList, you usually only use that for backwards compatibility, it is generally preferable to use List<T>, in this case a List<string>. Commented Oct 16, 2014 at 10:20
  • 2
    You need to use using blocks when you handle classes implementing IDisposable. If you have a Visual Studio that supports it, turn on static code analysis so you will get a warning when you forget it. Commented Oct 16, 2014 at 10:20
  • 1
    Use tw.Close(); at the end of your method. Commented Oct 16, 2014 at 10:24
  • @Ben Robinson I am using ArrayList because that is what my course requires me to do. Whether I move onto using List next I am not sure. Commented Oct 17, 2014 at 0:57
  • @nvoigt I will turn it on, thanks. I wasn't aware of this. Commented Oct 17, 2014 at 0:57

3 Answers 3

3

You need to Flush or Close your writer:

private void WriteArrayList()
{
    TextWriter tw = null;

    try
    {
        tw = File.CreateText("actors.txt");
        foreach (object o in ActorArrayList)
            tw.WriteLine(o.ToString());
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error saving file!");
    }
    finally
    {
        if(tw != null)
            tw.Close();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Should really be a comment, not an answer.
Now that's an answer! :)
I added a Close, using the finally block it gives me an error "Use of an unassigned local variable 'tw'". But if I place it directly after tw.WriteLine(o.ToString()); it works fine.
@mrkd1991: I edited the answer to solve this issue. good luck ;)
2

You can manage to do this in a 1 liner using linq Cast():

File.WriteAllLines(@"actors.txt", ActorArrayList.Cast<string>());

Alternatively, the below logic should work out

using (TextWriter writer = File.CreateText(@"actors.txt"))
{
    foreach (string actor in ActorArrayList)
    {
        writer.WriteLine(actor);
    }
}

Comments

0

You are missing a call to tw.Flush() and also a call to tw.Close(). Without calling these the content may not actually get written to disk but only persists in memory. Also in this case you should alwas use a using block as mentioned before.

See this example:

using (TextWriter tw = File.CreateText("actors.txt"))
{
   try
   {
      foreach (object o in ActorArrayList)
         tw.WriteLine(o.ToString());
      tw.Flush();
   }
   catch (Exception ex)
   {
      MessageBoxShow("Error saving file!");
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.