2

我一直在为一个大学项目做这个并且遇到了一个问题。我已设法从文件中加载多行,但无法将它们保存回文件。我可以将单个字符串保存到文件中,这是最后处理的字符串,但仅此而已。通过执行循环,我可能完全错了,但我想不出任何其他方法来做到这一点。保存文件部分的编码如下:

    case "s":
    case "8":
        {
            int savecount = 0;
            string savestring = "";

            //Clear the text file ready to be saved
            using (FileStream fs = File.Create("billing.txt"))
            {

            }

            while (savecount != CustomerCount)
                {
                    using (StreamWriter save = new StreamWriter("billing.txt"))
                        {
                //Create the string to save to the file
                            savestring = CustomerNumber[savecount] + ","
                              + CustomerName[savecount] + ","
                              + Address[savecount] + ","
                              + RateScheme[savecount] + ","
                              + PeakKWH[savecount] + ","
                              + OffPeakKWH[savecount] + ","
                              + StandardKWH[savecount];

                            Console.WriteLine(savestring);

                            save.WriteLine(savestring);

                            savecount++;
                            Console.ReadLine();
                          }
                 }
            Console.WriteLine("All data saved successfully");
            Console.ReadLine();
            break;
        }

不知道从这里去哪里。任何帮助,将不胜感激

4

4 回答 4

1

您应该在循环之前打开文件进行保存。例如

using (StreamWriter save = new StreamWriter("billing.txt")) {
    while (savecount != CustomerCount) { 
        // rest of your code here

目前,您正在每个循环中打开文件,并写出一行。然后重新打开它(并丢失已经写入的数据)。

正如评论中指出的那样,您不需要调用File.Create. 默认情况下,StreamWriter将覆盖现有文件。

于 2013-10-11T10:33:02.550 回答
1

您需要在其中的 while 循环,using { }因为您每次都在覆盖您的数据,当您查看它时将最后一项留在文件中:

using (StreamWriter save = new StreamWriter("billing.txt"))
{
     while (savecount != CustomerCount)
     {
       //Create the string to save to the file
       string savestring = CustomerNumber[savecount] + ","
       + CustomerName[savecount] + ","
       + Address[savecount] + ","
       + RateScheme[savecount] + ","
       + PeakKWH[savecount] + ","
       + OffPeakKWH[savecount] + ","
       + StandardKWH[savecount];

       Console.WriteLine(savestring);

       save.WriteLine(savestring);

       savecount++;
       Console.ReadLine();
   }
}
于 2013-10-11T10:35:08.643 回答
1

您做错的是,您在每次迭代中打开文件,在文件中写入一行,然后再次重新打开文件并覆盖内容。您可以重新更改您的代码

using (StreamWriter save = new StreamWriter("billing.txt")) 
{
   while (savecount != CustomerCount) 
   { 
     // rest of string formation of saveString logic and save.WriteLine(savestring); goes here
     .....
   }
}

我认为您也可以使用一个简单的代码,您可以将所有输入字符串保存在 List 中并使用 File.WriteAllLines 函数作为

     {
       ....   
        List<string> Customers = new List<string>();
        for (savecount = 0; savecount < CustomerCount; savecount++)
        {            
         //Create the string to save to the file
            Customers.Add( CustomerNumber[savecount] + "," + CustomerName[savecount] + "," + Address[savecount] + "," + RateScheme[savecount] + "," + PeakKWH[savecount] + "," + OffPeakKWH[savecount] + "," + StandardKWH[savecount]);

            Console.WriteLine(Customers[savecount]);
        }

        string filePath = "billing.txt"; // This is your file path where all the contents are to be written
        File.WriteAllLines(filePath, Customers);
       ..........
     }
于 2013-10-11T10:50:51.450 回答
0

你需要:

using (StreamWriter save = new StreamWriter("billing.txt")) {
    while (savecount != CustomerCount) {

您必须在循环之前打开文件,因为在里面打开会删除所有以前写入的数据,而且打开也需要一些时间。

但是你可以在循环中打开文件,但你需要设置append文件,它是:

StreamWriter save = new StreamWriter("billing.txt", true)

此选项速度较慢,您可能需要在以附加模式打开之前清除文件,因此它不是最佳选择。

于 2013-10-11T10:49:04.623 回答