从列表保存<;T>;到txt

本文关键字:gt txt lt 列表 保存 | 更新日期: 2023-09-27 18:20:31

我希望我的程序从两个文本文件读取到一个List<T>中。List<T>正在对重复项进行排序和清理。

我希望List<T>保存(排序和清理后)到一个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();
        }
    }
}

从列表保存<;T>;到txt

有一个方便的小方法File.WriteAllLines——无需自己打开StreamWriter

In.net 4:

File.WriteAllLines(speichern, ausgabeListe);

In.net 3.5:

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

同样,您可以将读取逻辑替换为File.ReadAllLines,它返回一个字符串数组(如果您想要List<string>,请使用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);

这一行写入了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();

循环浏览列表,分别写入每一行:

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

尝试以下代码:

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----------------");

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

正如您使用ForEach将内容写入Console一样,您需要对ausgabeListe进行迭代,为列表中的每个项调用WriteLine()

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

我正在使用类似下面的LINQ将每一行写入文本文件。

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