对.txt文件中的一些记录进行排序,而不会干扰其他记录并覆盖同一文件

时间:2017-08-18 03:49:56

标签: c# streamreader streamwriter

我试图逐行阅读文本文件并尝试按照价格按升序排序“Puma”命名鞋子,并将更改保存在同一文件中,而不会影响“Nike”和“Adidas”的顺序“鞋子。如果有人可以帮助我,那可能会很棒。提前谢谢。

以下是我尝试的代码

    public class Program
    {
      static string content;
      static string outputFile = @"C:\Users\Desktop\ShoeRack.txt";

      public static void Main(string[]args)
      {
        sorting();
      }
      public static void sorting()
      {
        try
        {
          string[] scores =System.IO.File.ReadAllLines(@"C:\Users\Desktop\ShoeRack.txt");

          var numbers = scores.OrderBy(x=>(x.Split(',')[2]));

          foreach(var dat in numbers)
         {
           content = dat.toString();
           writetoFile(outputFile,content);
         }

        }
        catch(Exception e)
        {
           Console.WriteLine(e);
           Console.ReadLine();
        } 
      }
      public static void writetoFile(string outputFile, string content)
      {
         using(System.IO.StreamWriter file = new System.IO.StreamWriter(@outputFile))
         {
            file.WriteLine(content);
         }
      }

    }

文本文件名称为ShoeRack.txt

ShoeCompany,Price,Size,Color,Avaibility
Nike,75,6,Red,Yes
Nike,80,5,Yellow,Yes
Nike,50,9,White,Yes
Nike,44,5,White,No
Adidas,50,7,Green,Yes
Adidas,55,8,Grey,Yes
Adidas,40,5,Red,Yes
Puma,50,7,Red,Yes
Puma,55,6,Yellow,Yes
Puma,44,5,Red,Yes
Puma,45,4,Green,No

输出

ShoeCompany,Price,Size,Color,Avaibility
Nike,75,6,Red,Yes
Nike,80,5,Yellow,Yes
Nike,50,9,White,Yes
Nike,44,5,White,No
Adidas,50,7,Green,Yes
Adidas,55,8,Grey,Yes
Adidas,40,5,Red,Yes
Puma,50,7,Red,Yes
Puma,55,6,Yellow,Yes
Puma,44,5,Red,Yes
Puma,45,4,Green,No
ShoeCompany,Price,Size,Color,Avaibility
Nike,75,6,Red,Yes
Nike,80,5,Yellow,Yes
Nike,50,9,White,Yes
Nike,44,5,White,No
Adidas,50,7,Green,Yes
Adidas,55,8,Grey,Yes
Adidas,40,5,Red,Yes
Puma,50,7,Red,Yes
Puma,55,6,Yellow,Yes
Puma,44,5,Red,Yes
Puma,45,4,Green,No 

预期输出

ShoeCompany,Price,Size,Color,Avaibility
Nike,75,6,Red,Yes
Nike,80,5,Yellow,Yes
Nike,50,9,White,Yes
Nike,44,5,White,No
Adidas,50,7,Green,Yes
Adidas,55,8,Grey,Yes
Adidas,40,5,Red,Yes
Puma,45,4,Green,No
Puma,44,5,Red,Yes
Puma,55,6,Yellow,Yes
Puma,50,7,Red,Yes

2 个答案:

答案 0 :(得分:0)

使用字符串构建器构建您的内容,然后在文件中添加该字符串。

 string[] scores =System.IO.File.ReadAllLines(@"C:\Users\Desktop\ShoeRack.txt");
                var content=new StringBuilder();  
              var numbers = scores.OrderBy(x=>(x.Split(',')[2]));

              foreach(var dat in numbers)
             {
               content = dat.toString();
               content.Append(content+Environment.NewLine);
             }
             writetoFile(outputFile,content.ToString());

最后使用

覆盖现有数据
System.IO.File.WriteAllText (@outputFile, contents);

答案 1 :(得分:0)

您编码的不是检查字符串是否为Puma

static string content;
    static string outputFile = @"C:\Users\Desktop\ShoeRack.txt";

    public static void sorting()
    {
        try
        {
            string[] scores = System.IO.File.ReadAllLines(@"C:\Users\Desktop\ShoeRack.txt");

            //Split into two lists:  one that is Puma, one that is the others
            List<string> pumaEntries = new List<string>();
            List<string> otherEntries = new List<string>();

            foreach (string item in scores)
            {
                if (item.Split(',')[0].ToUpper() == "PUMA")
                {
                    pumaEntries.Add(item);
                }
                else
                {
                    otherEntries.Add(item);
                }
            }
            //Now sort the puma entries
            var sorted = pumaEntries.OrderBy(x => x.Split(',')[1]);

            //Now output the "Other" entries, then the Puma ones
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@outputFile))
            {
                foreach (string dat in otherEntries)
                {
                    file.WriteLine(dat);
                }
                foreach (string dat in sorted)
                {
                    file.WriteLine(dat);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            Console.ReadLine();
        }
    }

此代码将Puma条目拆分为单独的列表以进行排序,并保持其他列表不变。

请注意,字符串数组是从零开始的,要按价格排序,您需要按[1]而不是[2]排序。