从列表保存到txt

发布于 2024-12-27 12:55:31 字数 1994 浏览 0 评论 0原文

我希望我的程序将两个文本文件读取到一个 List 中。 List 正在排序和清除重复项。

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

I want my program to read from two text files into one List<T>.
The List<T> is sorting and cleaning duplicates.

I want the List<T> to save (after sorting and cleaning) to a txt file.

But when I looked in the result txt file, I found this message:

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

Does anyone have an idea how I could fix this error?

Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦亿 2025-01-03 12:55:31

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

在 .net 4 中:

File.WriteAllLines(speichern, ausgabeListe);

在 .net 3.5 中:

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

同样,您可以用 File.ReadAllLines,返回字符串数组(使用 ToList()如果你想要一个List)。

因此,事实上,您的完整代码可以简化为:

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

There's a handy little method File.WriteAllLines -- no need to open a StreamWriter yourself:

In .net 4:

File.WriteAllLines(speichern, ausgabeListe);

In .net 3.5:

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

Likewise, you could replace your reading logic with File.ReadAllLines, which returns an array of strings (use ToList() on that if you want a List<string>).

So, in fact, your complete code could be reduced to:

// 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);
隱形的亼 2025-01-03 12:55:31

正是这一行写入了列表的 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();

It's this line which writes the ToString representation of the List, resulting into the text line you got:

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

Instead you want to write each line.

StreamWriter file = new System.IO.StreamWriter(speichern);
ausgabeListe.ForEach(file.WriteLine);
file.Close();
暖风昔人 2025-01-03 12:55:31

循环遍历列表,单独写入每一行:

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

Loop through the list, writing each line individually:

StreamWriter file = new System.IO.StreamWriter(speichern);
foreach(string line in ausgabeListe)
    file.WriteLine(line);
file.Close();
羞稚 2025-01-03 12:55:31

尝试下面的代码:

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

Try the code below:

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----------------");
流殇 2025-01-03 12:55:31

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

正如您使用 ForEach 将内容写入控制台一样,您需要迭代 ausgabeListe,为每个项目调用 WriteLine()名单。

You are writing the list object into the file, so you see the type name.

Just as you are using ForEach to write the contents to the Console, you need to iterate over ausgabeListe, calling WriteLine() for each item in the list.

北风几吹夏 2025-01-03 12:55:31
StreamWriter file = new System.IO.StreamWriter(speichern);
foreach(string x in ausgabeListe)
    file.WriteLine(x);
file.Close();
StreamWriter file = new System.IO.StreamWriter(speichern);
foreach(string x in ausgabeListe)
    file.WriteLine(x);
file.Close();
徒留西风 2025-01-03 12:55:31

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

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

I am using the LINQ like below to write each line to text file.

var myList=new List<string>
{
    "Hello",
    "World"
};
using (var file = new StreamWriter("myfile.txt"))
{
    myList.ForEach(v=>file.WriteLine(v));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文