C# 保存到文本文件中的多行

本文关键字:文件 保存 文本 | 更新日期: 2023-09-27 18:35:07

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

    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;
        }

不知道从这里开始。任何帮助将不胜感激

C# 保存到文本文件中的多行

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

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

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

正如评论中指出的,您无需致电File.Create. 默认情况下,StreamWriter将覆盖现有文件。

您需要在

using { }内使用 while 循环 因为它是你每次都会覆盖你的数据,当你查看它时,在你的文件中留下最后一项:

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();
   }
}

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

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

我认为您也可以使用一个简单的代码,您可以在其中将所有输入字符串保存在列表中,并使用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);
       ..........
     }

你需要:

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

您必须在循环之前打开文件,因为在内部打开会删除所有写入其中的先前数据,打开也需要一些时间。

但是,您可以在循环内打开文件,但您需要设置append文件,它将是:

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

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