C# 从 List<T> 保存到 txt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8883483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving from List<T> to txt
提问by Jon Cylo
I want my program to read from two text files into one List<T>.
The List<T>is sorting and cleaning duplicates.
我希望我的程序将两个文本文件读入一个List<T>. 的List<T>被分类和清洗重复。
I want the List<T>to save (after sorting and cleaning) to a txt file.
我想List<T>保存(排序和清理后)到一个 txt 文件。
But when I looked in the result txt file, I found this message:
但是当我查看结果txt文件时,我发现了这条消息:
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?
有谁知道我如何解决这个错误?
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();
}
}
}
回答by Matten
It's this line which writes the ToString representation of the List, resulting into the text line you got:
正是这一行写入了 List 的 ToString 表示,从而生成了您获得的文本行:
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();
回答by Heinzi
There's a handy little method File.WriteAllLines-- no need to open a StreamWriteryourself:
有一个方便的小方法File.WriteAllLines—— 不需要StreamWriter自己打开一个:
In .net 4:
在 .net 4 中:
File.WriteAllLines(speichern, ausgabeListe);
In .net 3.5:
在 .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>).
同样,你可以用File.ReadAllLines替换你的阅读逻辑,它返回一个字符串数组(ToList()如果你想要一个,就使用它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);
回答by itsme86
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();
回答by Jay
You are writing the list object into the file, so you see the type name.
您正在将列表对象写入文件,因此您可以看到类型名称。
Just as you are using ForEachto write the contents to the Console, you need to iterate over ausgabeListe, calling WriteLine()for each item in the list.
正如您ForEach用来将内容写入控制台一样,您需要遍历ausgabeListe,调用WriteLine()列表中的每个项目。
回答by watbywbarif
StreamWriter file = new System.IO.StreamWriter(speichern);
foreach(string x in ausgabeListe)
file.WriteLine(x);
file.Close();
回答by LENG UNG
I am using the LINQ like below to write each line to text file.
我正在使用如下所示的 LINQ 将每一行写入文本文件。
var myList=new List<string>
{
"Hello",
"World"
};
using (var file = new StreamWriter("myfile.txt"))
{
myList.ForEach(v=>file.WriteLine(v));
}
回答by Prabhakaran M
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----------------");

