2013-10-15 40 views
-4

我有一個大約3毫米行的文件。每行包含這樣的記錄:閱讀並從文件中提取

1|2|3|4|5|6|7|8|9 

正確的8個分隔符,如'|'在每一行上。我正在尋找一種方法來讀取這個文件,然後從每一行中提取最後的'9'號碼並將其存儲到另一個文件中。

編輯:

確定這裏就是我已經做了。

using (StreamReader sr = new StreamReader(filepath)) 
    using (StreamWriter sw = new StreamWriter(filepath1)) 
    { 
     string line = null; 
     while ((line = sr.ReadLine()) != null) 
      sw.WriteLine(line.Split('|')[8]); 
    } 

File.WriteAllLines("filepath", File.ReadAllLines(filepath).Where(l => !string.IsNullOrWhiteSpace(l))); 

讀取文件,提取最後的數字,然後寫入新文件並清空空白行。最後一位數字是10-15個符號,我想先提取6.我繼續閱讀並嘗試一些,當我完成或有一些問題時,我會再次編輯。

感謝

編輯2: 好吧,這裏我先取8位從數量:

sw.WriteLine(line.Substring(0, Math.Min(line.Length, 8))); 

編輯3: 我不知道我該怎麼現在左每個號碼相匹配文件。我想匹配他們,並看到女巫號碼在文件中有多少次。

任何幫助?

+10

可愛。玩的開心。如果您有任何問題,請務必告訴我們。 – noelicus

+0

我剛剛開始尋找一些想法或教程,因爲我不是很高級的C#。謝謝 – Goro

+0

祝你好運!一次只能走一步:將它分解成小任務,當你感到高興時,第一批作品將進入下一階段。享受:) – noelicus

回答

1
using (StreamReader sr = new StreamReader("input")) 
    using (StreamWriter sw = new StreamWriter("output")) 
    { 
     string line = null; 
     while ((line=sr.ReadLine())!=null) 
      sw.WriteLine(line.Split('|')[8]); 
    } 
+1

這會寫一行:) – Damith

+1

嘿,沒有想到就打出來了。 :)我現在糾正它。 –

3

我正在尋找一種方法來閱讀這個文件,然後提取最後[..]號碼只從每一行,並將其存儲到另一個文件。

你到底有什麼問題嗎?在僞代碼,這是你想要什麼:

fileReader = OpenFile("input") 
fileWriter = OpenFile("output") 

while !fileReader.EndOfFile 
    line = fileReader.ReadLine 
    records[] = line.Split('|') 
    value = records[8] 
    fileWriter.WriteLine(value) 
do 

所以開始實施它,隨便問一個問題,你遇到麻煩的任何特定行。我發佈的每行代碼都包含足夠的指針來指出C#代碼或用於網頁搜索的術語。

+0

太棒了!感謝您的出發點! – Goro

1

使用String.Split()獲取數組中的行並獲取最後一個元素並將其存儲到另一個文件中。重複每一行的過程。

2

你不說你卡在哪裏。打破這個問題:

Write and run minimal C# program 

Read lines from file 

Break up one line 

write result line to a file 

你卡在其中的任何一個?然後問一個具體的問題。這種分解技術是許多編程任務的關鍵,而且一般來說的確是複雜的任務。

您可能會發現string split功能很有用。

1

使用LINQ,你可以做的事情類似下面的篩選數字:

var numbers = from l in File.ReadLines(fileName) 
       let p = l.Split('|') 
       select p[8]; 

,然後將它們寫入這樣的一個新的文件:

File.WriteAllText(newFileName, String.Join("\r\n", numbers)); 
+0

請注意,有〜3磨坊行! – Damith

+0

這不是問題,File.ReadLines()以惰性方式返回IEnumerable。 – Flea777

+1

不知道如何調用具有〜3磨記錄的'String.Join'時會發生什麼 – Damith

1

試試這個。 ...

// Read the file and display it line by line. 
System.IO.StreamReader file = 
    new System.IO.StreamReader("c:\\test.txt"); 
while((line = file.ReadLine()) != null) 
{ 
    string[] words = s.Split('|'); 
    string value = words [8] 
    Console.WriteLine (value); 

} 

file.Close(); 
1

因爲它是一個巨大的文件,你必須逐行讀取它!

public IEnumerable ReadFileIterator(String filePath) 
     { 
      using (StreamReader streamReader = new StreamReader(filePath, Encoding.Default)) 
      { 
       String line; 
       while ((line = streamReader.ReadLine()) != null) 
       { 
        yield return line; 
       } 
       yield break; 
      } 
     } 

     public void WriteToFile(String inputFilePath, String outputFilePath) 
     { 
      using (StreamWriter streamWriter = new StreamWriter(outputFilePath, true, Encoding.Default)) 
      { 
       foreach (String line in ReadFileIterator(inputFilePath)) 
       { 
        String[] subStrings = line.Split('|'); 
        streamWriter.WriteLine(subStrings[8]); 
       } 
       streamWriter.Flush(); 
       streamWriter.Close(); 
      } 
     }