2013-02-27 165 views
0

我有一個文本文件,其中有很多的信息行,我試圖做的是搜索每一行,以_開頭,一旦我找到這條線複製下面6行的行,並將其添加到第1行在下方行並刪除其他行信息。編輯文本文件?

例如:

 
_ 
Blue 
X 
X 
X 
X 
10.5 
X 
X 
X 
_ 
Orange 
X 
X 
X 
X 
15.1 
X 
X 
X 
_ 

我想作爲

 
Blue 10.5 
Orange 15.1 

我目前使用VS 2010

顯示

回答

2

使用IO.StreamReader讀取文件的行:

Dim sr As New StreamReader(sFilePath) 

件然後循環線路和做的事情,你問:

Dim sLine, sResult As String 
Dim iCont As Integer = -1 

sResult = "" 
sLine = sr.ReadLine() 

While Not sr.EndOfStream 
    If sLine.StartsWith("_"c) Then iCont = 0 

    Select Case iCont 
     Case 1 
      sResult &= sLine & " " 
     Case 6 
      sResult &= sLine & Environment.NewLine() 
    End Select 

    If iCont >= 0 Then iCont += 1 
    sLine = sr.ReadLine() 
End While 

sr.Close() 
MessageBox.Show(sResult) 

編輯:
如果你想將其寫入到文件,然後:

Dim sw As New StreamWriter(sNewFilePath) 

sw.Write(sResult) 
sw.Close() 

如果您並不需要別的,你也可以這樣做:

IO.File.WriteAllText(sNewFilePath, sResult) 


文檔

+0

謝謝,我試了一下你的建議,並在消息框中顯示結果,我需要有一個StreamWriter保存它到一個新的文本文件? – JackSparrow 2013-02-27 11:19:25

+0

聲明一個StreamWriter並寫入它。我編輯我的答案。 – SysDragon 2013-02-27 11:23:12

+0

非常感謝:)每天學習新東西 – JackSparrow 2013-02-27 11:30:45