2015-05-06 59 views
-1

在文本文件中的行應該是這樣的:如何將文本文件讀取到c#中的列表?

1056 Mark Supers Swagminator [email protected] 3500 

他們都必須進入自己的房產爲一個新的對象,然後下一個行會進入自己的對象。如何才能做到這一點?

這被認爲與「賬戶」名單的工作,這是賬戶信息,與名稱等。

+0

你得找出你想要的數據之間的分隔符移動。它是一個製表符(「\ t」)嗎? –

+0

什麼是類的樣子,你想要把每一行成? –

+0

分隔符不能是它們之間的空格嗎?否則我不知道我明白這個問題。 – vality

回答

0

你可以用Google搜索是很容易的,我認爲。但這是一個可以輕鬆完成的方法。該表揚應該解釋我在做什麼。

// Create Class that holds the Attributes for you 
    // I am using auto properties here 
    // if you don't know what a property is pls google it - you need to know that 
    class AccountData 
    { 
     public int firstNumber { get; set; } 

     public string firstString { get; set; } 

     public string secondString { get; set; } 

     public string thirdString { get; set; } 

     public string mailAddress { get; set; } 

     public int lastNumber { get; set; } 

    } 

    List<AccountData> Parser(string FileLocationWithName) 
    { 

     // FileLocationWithName is something like "C:\MyFolder\MyFile.txt" 
     // If you want to write a backsplash (\) you need to write \\ 
     // or you can use a @ bevore the string 
     // without @: "C:\\MyFolder\\MyFile.txt" 
     // with @: @"C:\MyFolder\MyFile.txt" 

     // Create your list 
     List<AccountData> resultList = new List<AccountData>(); 

     // Oben a new FileStream - a StreamReader is good 
     using (StreamReader sr = new StreamReader(FileLocationWithName)) 
     { 

      // Read the Whole file <=> sr is not at the end of the stream 
      while (!sr.EndOfStream) 
      { 
       // read a line and split it into the strings 
       string line = sr.ReadLine(); 

       var elementsOfLine = line.Split(' '); 

       // create a new object of your accountData class and fill it with the string elements 
       var tempElement = new AccountData(); 
       tempElement.firstNumber = int.Parse(elementsOfLine[0]); 
       tempElement.firstString = elementsOfLine[1]; 
       tempElement.secondString = elementsOfLine[2]; 
       tempElement.thirdString = elementsOfLine[3]; 
       tempElement.mailAddress = elementsOfLine[4]; 
       tempElement.lastNumber = int.Parse(elementsOfLine[5]); 

       resultList.Add(tempElement); 
      } 
     } 

     // return your list 
     return resultList; 
    } 
0

來自:Reading a text file word by word

using (var mappedFile1 = MemoryMappedFile.CreateFromFile(filePath)) 
    { 
     using (Stream mmStream = mappedFile1.CreateViewStream()) 
     { 
      using (StreamReader sr = new StreamReader(mmStream, ASCIIEncoding.ASCII)) 
      { 
       while (!sr.EndOfStream) 
       { 
        string line = sr.ReadLine(); 
        string lineWords = line.Split(' '); 
        number = lineWords[0]; 
        name = lineWords[1]; //and so on.. 
       } 
      } 
     } 
    } 
+1

爲什麼你會使用內存映射文件? –

0
string line = "1056%Mark%Supers%Swagminator%[email protected]%3500"; 
var propertyArray = line.Split('%'); 
foreach (string entry in propertyArray) 
{ 
    Console.WriteLine(entry); 
} 

這將打印的每個條目。您可以將每個條目用作數組中的簡單對象。 propertyArray[0]將是1056

只是要小心的大小。如果有人有4名您的電子郵件地址會從propertyArray[4]propertyArray[5]

相關問題