【问题标题】:Saving from List<T> to txt从 List<T> 保存到 txt
【发布时间】:2012-01-16 17:01:00
【问题描述】:

我希望我的程序将两个文本文件读入一个List&lt;T&gt;List&lt;T&gt; 正在对重复项进行排序和清理。

我希望 List&lt;T&gt; 保存(在排序和清理之后)到一个 txt 文件。

但是当我查看结果 txt 文件时,我发现了这条消息:

System.Collections.Generic.List`1[System.String]

有人知道我该如何解决这个错误吗?

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Uniqpass
{
    class Program
    {
        static void Main(string[] args)
        {

            String pfad = "C:\\Dokumente und Einstellungen\\Bektas\\Desktop\\test\\";
            String pfad2 = "C:\\Dokumente und Einstellungen\\Bektas\\Desktop\\test\\";
            String speichern = "C:\\Dokumente und Einstellungen\\Bektas\\Desktop\\test\\ausgabe.txt";
            String datei = "text1.txt";
            String datei2 = "text2.txt";

            try
            {

                //Einlesen TxT 1
                List<String> pass1 = new List<String>();
                StreamReader sr1 = new StreamReader(pfad + datei);
                while (sr1.Peek() > -1)
                {

                    pass1.Add(sr1.ReadLine());
                }
                sr1.Close();
                //Einlesen TxT 2
                StreamReader sr2 = new StreamReader(pfad2 + datei2);
                while (sr2.Peek() > -1)
                {
                    pass1.Add(sr2.ReadLine());
                }
                sr2.Close();

                List<String> ausgabeListe = pass1.Distinct().ToList();
                ausgabeListe.Sort();

                ausgabeListe.ForEach(Console.WriteLine);

                StreamWriter file = new System.IO.StreamWriter(speichern);
                file.WriteLine(ausgabeListe);
                file.Close();


            }
            catch (Exception)
            {
                Console.WriteLine("Error");
            }

            Console.ReadKey();
        }
    }
}

【问题讨论】:

    标签: c# save generic-list


    【解决方案1】:

    有一个方便的小方法File.WriteAllLines——不用自己开StreamWriter

    在 .net 4 中:

    File.WriteAllLines(speichern, ausgabeListe);
    

    在 .net 3.5 中:

    File.WriteAllLines(speichern, ausgabeListe.ToArray());
    

    同样,您可以用File.ReadAllLines 替换您的阅读逻辑,它会返回一个字符串数组(如果您想要List&lt;string&gt;,请使用ToList())。

    所以,事实上,你的完整代码可以简化为:

    // Input
    List<String> data = File.ReadAllLines(pfad + datei)
        .Concat(File.ReadAllLines(pfad2 + datei2))
        .Distinct().ToList();
    
    // Processing
    data.Sort(); 
    
    // Output
    data.ForEach(Console.WriteLine); 
    File.WriteAllLines(speichern, data);
    

    【讨论】:

    • 很棒的答案。正是我需要的。
    • 这个答案很棒,因为它可能将几行代码减少到一行。
    • @Free2Rhyme2k:是的,更少的代码行通常意味着更少的错误。
    【解决方案2】:

    正是这一行写入了 List 的 ToString 表示,从而形成了你得到的文本行:

    StreamWriter file = new System.IO.StreamWriter(speichern);
    file.WriteLine(ausgabeListe);
    file.Close();
    

    你想写每一行。

    StreamWriter file = new System.IO.StreamWriter(speichern);
    ausgabeListe.ForEach(file.WriteLine);
    file.Close();
    

    【讨论】:

      【解决方案3】:

      遍历列表,分别写下每一行:

      StreamWriter file = new System.IO.StreamWriter(speichern);
      foreach(string line in ausgabeListe)
          file.WriteLine(line);
      file.Close();
      

      【讨论】:

        【解决方案4】:

        试试下面的代码:

        StreamWriter writer = new StreamWriter("C:\\Users\\Alchemy\\Desktop\\c#\\InputFileFrmUser.csv");
        list = new List<Product>() { new Product() { ProductId=1, Name="Nike 12N0",Brand="Nike",Price=12000,Quantity=50},
                new Product() { ProductId =2, Name = "Puma 560K", Brand = "Puma", Price = 120000, Quantity = 55 },
                new Product() { ProductId=3, Name="WoodLand V2",Brand="WoodLand",Price=21020,Quantity=25},
                new Product() { ProductId=4, Name="Adidas S52",Brand="Adidas",Price=20000,Quantity=35},
                new Product() { ProductId=5, Name="Rebook SPEED2O",Brand="Rebook",Price=1200,Quantity=15}};
        
        foreach (var x in list) {
            string wr = x.ProductId + " " + x.Name + "" + x.Brand + " " + x.Quantity + " " + x.Price;
            writer.Flush();
            writer.WriteLine(wr);
        
        }
        Console.WriteLine("--------ProductList Updated SucessFully----------------");
        

        【讨论】:

          【解决方案5】:

          您正在将列表对象写入文件,因此您会看到类型名称。

          正如您使用ForEach 将内容写入控制台一样,您需要遍历ausgabeListe,为列表中的每个项目调用WriteLine()

          【讨论】:

            【解决方案6】:
            StreamWriter file = new System.IO.StreamWriter(speichern);
            foreach(string x in ausgabeListe)
                file.WriteLine(x);
            file.Close();
            

            【讨论】:

            • 您能否给变量命名,例如“speichern”和“ausgabeListe”,以便于理解?
            【解决方案7】:

            我正在使用如下所示的 LINQ 将每一行写入文本文件。

            var myList=new List<string>
            {
                "Hello",
                "World"
            };
            using (var file = new StreamWriter("myfile.txt"))
            {
                myList.ForEach(v=>file.WriteLine(v));
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-12-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多