閱讀后,我需要從 txt 中洗掉一行。逐行閱讀我使用
Dim srdFile As IO.StreamReader
srdFile = New IO.StreamReader("shazam.txt")
Do Until srdFile.Peek = -1
strLine = srdFile.ReadLine()
... how to delete the line here..
' here i call another function
Loop
srdFile.Close()
如何在閱讀后將其洗掉?另外,每次洗掉一行時如何保存檔案?謝謝
uj5u.com熱心網友回復:
這不是一個好主意。您必須在閱讀后洗掉第一行,然后將所有行移到頂部,因此基本上您必須在每一行中重寫整個檔案。那不是你想要的。但是您可以處理每一行并確定哪些無法成功處理。然后您可以清除檔案并將剩余的行寫入該檔案:
Dim failResults = From line In IO.File.ReadLines(path)
Select result = (Line:=line, ProcessResult:=ProcessLine(line))
Where result.ProcessResult = False
Select result.Line
Dim remainingLines = failResults.ToList()
IO.File.WriteAllText(path, "")
IO.File.WriteAllLines(path, remainingLines)
這是您必須填寫邏輯的方法:
Private Function ProcessLine(line As String) As Boolean
' do something with this line and return if it was successful, so can be deleted
Return True
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503725.html