繁体   English   中英

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

[英]C# save to multiple lines in a text file

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

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

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

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

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

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

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

您需要在using { }内部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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM