【问题标题】:C# - Can't write list into the txt fileC# - 无法将列表写入 txt 文件
【发布时间】:2015-06-12 01:43:09
【问题描述】:

我在将列表写入 txt 文件时遇到问题。如果我运行我的 Save 方法,它只会生成空白的 txt 文件。我从 txt 文件中填写了这个列表,它工作正常,所以我确定它不是空的(我可以在日历中看到我的约会)。有我的方法。

编辑

好的,我知道问题出在哪里。加载中的_appointments 列表与Save 中的_appointments 列表不同。我不知道为什么。我没有其他清单。它是一样的,但它不是:/

    public bool Load()
    {
        DateTime start = new DateTime(2000,01,01);
        CultureInfo enUS = new CultureInfo("en-US");
        int length = 0;
        string screenDiscription = "";
        bool occursOnDate = false;
        string line;
        int i = 1;
        StreamReader sr = new StreamReader("appointments.txt");

        if (!File.Exists("appointments.txt"))
        {
            return false;
        }

        while ((line = sr.ReadLine()) != null)
        {
            if (i % 4 == 1)
            {
                start = DateTime.ParseExact(line, "ddMMyyyy HHmm", enUS);
            }
            if (i % 4 == 2)
            {
                length = int.Parse(line);
            }
            if (i % 4 == 3)
            {
                screenDiscription = line;
            }
            if (i % 4 == 0)
            {
                Appointment appointment = new Appointment(start, length, screenDiscription, occursOnDate);
                _appointments.Add(appointment);
            }
            i++;
        }
        sr.Close();
        return true;
    }

    public bool Save()
    {
        StreamWriter sw = new StreamWriter("appointments.txt");

        if (File.Exists("appointments.txt"))
        {
            foreach(IAppointment item in _appointments)
            {
                    sw.WriteLine(item.Start);
                    sw.WriteLine(item.Length);
                    sw.WriteLine(item.DisplayableDescription);
                    sw.WriteLine(" ");
            }
            sw.Close();
            return true;
        }
        else
        {
            File.Create("appointments.txt");

            foreach (IAppointment item in _appointments)
            {
                    sw.WriteLine(item.Start);
                    sw.WriteLine(item.Length);
                    sw.WriteLine(item.DisplayableDescription);
                    sw.WriteLine(" ");
            }
            sw.Close();
            return true;
        }
    }

【问题讨论】:

  • 您是否尝试过创建TextWriter 而不是StreamWriter
  • 使用 TextWritier 会让你受益更多。
  • @BradleyWilson 我认为StreamWriter 可以处理字节,不是吗?
  • 在关闭 StreamWriter 之前尝试使用 sw.Flush()。
  • StreamWriter(string) 构造函数的documentation 表示,如果文件不存在,它将创建文件,如果无法创建,则会抛出异常。因此,您无需检查它是否存在或自己创建它。

标签: c#


【解决方案1】:

我已经重构了你的Save 方法,但我无法测试它,因为我没有你的IAppointmentAppointment

public void Save()
{
    var builder = new StringBuilder()
    foreach (IAppointment item in _appointments)
    {
        builder.AppendLine(item.Start);
        builder.AppendLine(item.Length);
        builder.AppendLine(item.DisplayableDescription);
        builder.AppendLine(" ");
    }

    File.WriteAllText("appointments.txt", builder.ToString());
}

这里注意几点:我认为您的bool 返回类型是多余的,因为该方法总是在所有代码路径上返回true;因此我将其更改为void。另外,我使用StringBuilder 来构造文件内容,然后使用内置的File.WriteAllText 方法抽象出您通常必须处理的打开流、流写入器、关闭的IO 操作等。

我不确定这是否能解决您的问题,因为正如我所说,我无法对其进行测试,并且我不确定您的代码到底有什么问题,但至少它可能是更清洁,更易于使用。

【讨论】:

  • 感谢您的回复。我的 Save 方法必须是 bool,因为我的评估需要它。我调试了它,_appointments 列表有问题。它是空的。我不知道为什么,因为我从文件中加载它并且我可以在我的日历中使用它但是当我想将它写入文件时它是空的..
  • “我的 Save 方法必须是 bool,因为我的 assingment 需要它”,如果它总是返回 true,我真的不知道该说什么了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多