从列表保存到txt
我希望我的程序将两个文本文件读取到一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有一个方便的小方法 File.WriteAllLines - 无需打开a
StreamWriter
自己:在 .net 4 中:
在 .net 3.5 中:
同样,您可以用 File.ReadAllLines,返回字符串数组(使用
ToList()
如果你想要一个List
)。因此,事实上,您的完整代码可以简化为:
There's a handy little method File.WriteAllLines -- no need to open a
StreamWriter
yourself:In .net 4:
In .net 3.5:
Likewise, you could replace your reading logic with File.ReadAllLines, which returns an array of strings (use
ToList()
on that if you want aList<string>
).So, in fact, your complete code could be reduced to:
正是这一行写入了列表的 ToString 表示形式,从而生成了您得到的文本行:
相反,您想要写入每一行。
It's this line which writes the ToString representation of the List, resulting into the text line you got:
Instead you want to write each line.
循环遍历列表,单独写入每一行:
Loop through the list, writing each line individually:
尝试下面的代码:
Try the code below:
您正在将列表对象写入文件,因此您会看到类型名称。
正如您使用
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 overausgabeListe
, callingWriteLine()
for each item in the list.我正在使用如下所示的 LINQ 将每一行写入文本文件。
I am using the LINQ like below to write each line to text file.