2013-11-23 70 views
1

所以開始這是我已經寫代碼:逐行讀取文本行進行字符串操作

 Dim MyFile As String = "Path" 
     Dim str_new As String 
     Dim str_old As String = File.ReadAllText(MyFile) 

     Dim sr As New StreamReader(MyFile) 
     Dim strLines As String() = Strings.Split(sr.ReadToEnd, Environment.NewLine) 
     Dim Character As Integer = 5 'Line 1 always has 5 characters 
     For i = 2 To strLines.Length 
      Dim PreviousLine As String = sr.ReadLine(i - 1) 
      Dim CurrentLine As String = sr.ReadLine(i) 
      If CurrentLine.Contains(TextBox1.Text/100) Then 
       If PreviousLine.Contains("divide") Then 
        Exit For 
       End If 
      End If 
      Character = Character + CurrentLine.Length 
     Next 
     sr.Close() 

     str_new = Replace(str_old, (TextBox1.Text/100), (TextBox3.Text/100), Character, 1) 

      Dim objWriter3 As New System.IO.StreamWriter(MyFile, False) 
      objWriter3.Write(str_new) 
      objWriter3.Flush() 
      objWriter3.Close() 

我試圖想出一個辦法來將一個長代碼文件到行,然後檢查每個爲某些字符串行。如果當前行包含字符串,那麼我會在上面和/或下面的行上進行額外的檢查,以確保這是字符串的正確實例。最後,我想用不同的字符串替換該字符串的實例。

一個例子:文本文件

class 
... 
0.3 
divide <-- Previous Line 
0.3  <-- TextBox1.Text is 30 
.5 
end 
  1. 我想要的代碼去過去的0.3
  2. 一審查找二審
  3. 檢查前行的鴻溝
  4. 退出循環
  5. 將0.3的第二個實例替換爲某個值

我一直在尋找這一段時間現在和任何幫助將不勝感激! 〜馬特

修訂:代碼

 Dim MyFile As String = "Path" 
     Dim NewFile As String = "Temporary Path" 
     Dim PreviousLine As String = "" 
     Dim CurrentLine As String = "" 
     Using sr As StreamReader = New StreamReader(MyFile) 
      Using sw As StreamWriter = New StreamWriter(NewFile) 
       CurrentLine = sr.ReadLine 
       Do While (Not CurrentLine Is Nothing) 
        Dim LinetoWrite = CurrentLine 

        If CurrentLine.Contains(TextBox1.Text) Then 
         If PreviousLine.Contains("divide") Then 
          LinetoWrite = Replace(CurrentLine, TextBox1.Text, TextBox3.Text) 
         End If 
        End If 

        sw.WriteLine(LinetoWrite) 
        PreviousLine = CurrentLine 
        CurrentLine = sr.ReadLine 
       Loop 
      End Using 
     End Using 

     My.Computer.FileSystem.CopyFile(NewFile, MyFile, True) 
+0

首先使用變量,而不是文本框。 'TextBox1.Text/100'就是不好的; TB包含文本而不是數字。由於您正在使用結果進行比較,因此您應該對結果進行格式化以使其匹配。另外,使用NotePad ++可能會更快。 – Plutonix

+0

什麼版本的.Net? –

+0

Plutonix〜我正在編寫一個應用程序,需要用戶輸入當前值和後一個值。我有錯誤處理,以確保字符串只有數字。 – Matt

回答

1

你在錯誤的道路面臨的問題。您必須同時設置讀取器和寫入器,並生成包含所有修改的新文件。你的代碼有不同的部分,應該改進;在這個答案中,我只是展示瞭如何正確使用StreamReader/StreamWriter。示例代碼:

Dim MyFile As String = "input path" 
Dim OutputFile As String = "output path" 
Dim PreviousLine As String = "" 
Dim CurrentLine As String = "" 
Using sr As StreamReader = New StreamReader(MyFile) 
    Using sw As StreamWriter = New StreamWriter(OutputFile) 

     CurrentLine = sr.ReadLine 

     Do While (Not CurrentLine Is Nothing) 

      Dim lineToWrite = CurrentLine 

      'Perform the analysis you wish by involving the current line, the previous one and as many other (previous) lines as you wish; and store the changes in lineToWrite. You should call a function here to perform this analysis 

      sw.WriteLine(lineToWrite) 'Writing lineToWrite to the new file 

      PreviousLine = CurrentLine 'Future previous line 
      CurrentLine = sr.ReadLine 'Reading the line for the next iteration 
     Loop 
    End Using 
End Using 
+0

我喜歡你的思維過程。閱讀和寫入新行是非常有意義的,而不是在流程結束時進行。考慮到這一點,我會重新修改我的代碼。 – Matt

+0

@Matt謝謝。這裏有所幫助。 – varocarbas

+0

更新了原始代碼並標記爲最佳答案。謝謝你,先生! – Matt