Saving from Listlt;Tgt; to txt(从列表中保存lt;Tgt;到txt)
问题描述
我希望我的程序将两个文本文件读入一个 List<T>.List<T> 正在对重复项进行排序和清理.
I want my program to read from two text files into one List<T>.
The List<T> is sorting and cleaning duplicates.
我希望 List<T> 保存(在排序和清理之后)到 txt 文件.
I want the List<T> to save (after sorting and cleaning) to a txt file.
但是当我查看结果 txt 文件时,我发现了这条消息:
But when I looked in the result txt file, I found this message:
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]
有人知道我该如何解决这个错误吗?
Does anyone have an idea how I could fix this error?
这是我的代码:
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();
        }
    }
}
推荐答案
有个方便的小方法 File.WriteAllLines -- 无需自己打开StreamWriter:
There's a handy little method File.WriteAllLines -- no need to open a StreamWriter yourself:
在 .net 4 中:
In .net 4:
File.WriteAllLines(speichern, ausgabeListe);
在 .net 3.5 中:
In .net 3.5:
File.WriteAllLines(speichern, ausgabeListe.ToArray());
同样,您可以将阅读逻辑替换为 File.ReadAllLines,它会返回一个数组字符串(使用 ToList() 如果你想要一个 List<string>).
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);
                        这篇关于从列表中保存<T>到txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从列表中保存<T>到txt
				
        
 
            
        - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - C# 中多线程网络服务器的模式 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 
						
						
						
						
						
				
				
				
				