T&GT;从列表与LT保存;为txt [英] Saving from List<T> to txt
问题描述
我希望我的程序从两个文本文件中读取到一个列表&LT; T&GT;
。
在列表&LT; T&GT;
正在梳理和清理重复
我要的列表&LT; T&GT;
保存到一个txt文件(分拣和清洗之后)
但是,当我在结果txt文件看,我发现这样的信息:
System.Collections.Generic.List`1 [System.String]
块引用>有没有人有一个想法,我怎么能解决这个问题?
下面是我的code:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.IO;命名空间Uniqpass
{
类节目
{
静态无效的主要(字串[] args)
{ 字符串PFAD =C:\\\\ Dokumente UND Einstellungen \\\\ \\\\ Bektas桌面\\\\ \\\\测试;
字符串pfad2 =C:\\\\ Dokumente UND Einstellungen \\\\ \\\\ Bektas桌面\\\\ \\\\测试;
字符串speichern =C:\\\\ Dokumente UND Einstellungen \\\\ \\\\ Bektas桌面\\\\ \\\\测试ausgabe.txt
字符串datei =text1.txt;
字符串datei2 =text2.txt; 尝试
{ // Einlesen TXT 1
清单&LT;串GT; PASS1 =新的List&LT;串GT;();
StreamReader的SR1 =新的StreamReader(PFAD +文件)的;
而(sr1.Peek()-1个)
{ pass1.Add(sr1.ReadLine());
}
sr1.Close();
// Einlesen TXT 2
StreamReader的SR2 =新的StreamReader(pfad2 + datei2);
而(sr2.Peek()-1个)
{
pass1.Add(sr2.ReadLine());
}
sr2.Close(); 清单&LT;串GT; ausgabeListe = pass1.Distinct()了ToList()。
ausgabeListe.Sort(); ausgabeListe.ForEach(Console.WriteLine); StreamWriter的文件=新System.IO.StreamWriter(speichern);
file.WriteLine(ausgabeListe);
file.Close();
}
赶上(例外)
{
Console.WriteLine(错误);
} Console.ReadKey();
}
}
}
解决方案有一个方便的小方法<一href=\"http://msdn.microsoft.com/en-us/library/system.io.file.writealllines.aspx\">File.WriteAllLines - 无需打开
的StreamWriter
自己:在.NET 4:
File.WriteAllLines(speichern,ausgabeListe);
在.NET 3.5:
File.WriteAllLines(speichern,ausgabeListe.ToArray());
同样,你可以更换 File.ReadAllLines 你的阅读的逻辑,它返回一个数组字符串(使用
了ToList()
上,如果你想有一个列表&LT;串&GT;
)。所以,其实,你完全code可以简化为:
//输入
清单&LT;串GT;数据= File.ReadAllLines(PFAD +文件)的
.Concat(File.ReadAllLines(pfad2 + datei2))
.Distinct()了ToList()。//处理
data.Sort();//输出
data.ForEach(Console.WriteLine);
File.WriteAllLines(speichern,数据);I want my program to read from two text files into one
List<T>
. TheList<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(); } } }
解决方案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 aList<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&GT;从列表与LT保存;为txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!