问题
So my program needs to go through a plain text file line by line essentially:
Read line 1:
Do commands
loop
Read line2:
Do Commands
loop
etc until its done with the entire file does anyone know any good coding examples for this, all the tutorials seem to show open and writing/reading textfiles but nothing on how to do it line by line.
回答1:
For Each line As String In System.IO.File.ReadAllLines("file.txt")
' Do Something'
Next
回答2:
You could do it like this:
Using f As System.IO.FileStream = System.IO.File.OpenRead("somefile.txt")
Using s As System.IO.StreamReader = New System.IO.StreamReader(f)
While Not s.EndOfStream
Dim line As String = s.ReadLine
'put you line processing code here
End While
End Using
End Using
来源:https://stackoverflow.com/questions/283156/going-through-a-text-file-line-by-line-in-vb-2005