2012-01-16 4 views
24

내 프로그램이 두 개의 텍스트 파일을 하나의 List<T>으로 읽으 려합니다. List<T>은 중복 된 항목을 정렬하고 정리합니다.리스트에서 저장 <T> to txt

List<T> (정렬 및 청소 후)을 txt 파일에 저장하려고합니다.

하지만 결과 txt 파일에서 보았을 때, 나는이 메시지를 발견

System.Collections.Generic.List`1는 [선택 System.String]

누구 아이디어가 있습니까을 이 오류를 어떻게 해결할 수 있습니까?

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

가 대신 각 줄을 쓰고 싶은 : 그것은 당신이있어 텍스트 행에 결과 목록의 toString 표현을 씁니다이 줄의

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

답변

10

: 여기

내 코드입니다 .

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

쉽고 간단한 방법 File.WriteAllLines에게 없다 - 열 필요가 StreamWriter 자신 :

File.WriteAllLines(speichern, ausgabeListe); 

.NET 3.5 :

File.WriteAllLines(speichern, ausgabeListe.ToArray()); 
.NET 4에서

마찬가지로 읽기 논리를 File.ReadAllLines으로 바꿀 수 있습니다. , 이는 문자열 배열을 반환합니다 (List<string>을 원할 경우 ToList()을 사용하십시오).

그래서, 사실, 당신의 전체 코드는 감소 될 수있다 : 개별적으로 각 라인을 작성, 목록을

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

환상적인 답변입니다. 정확히 내가 필요로하는 것. –

+2

이 답변은 잠재적으로 여러 줄의 코드를 한 줄로 줄이기 때문에 좋습니다. – Free2Rhyme2k

+0

@ Free2Rhyme2k : 예, 코드 줄이 적어 대개 버그가 적습니다. – Heinzi

1

루프 :

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

당신은 파일에 목록 개체를 작성하는 , 그래서 당신은 타입 이름을 볼 수 있습니다.

ForEach을 콘솔에 쓰는 것처럼 ausgabeListe을 반복하고 목록의 각 항목에 대해 WriteLine()을 호출해야합니다.

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

"speichern"및 "ausgabeListe"와 같은 변수의 이름을 쉽게 이해할 수 있습니까? – Soyokaze

0

텍스트 파일에 각 줄을 쓰려면 아래처럼 LINQ를 사용하고 있습니다.

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