我有一个从文件中读取的函数,另一个是写入文件的函数。我要读取和写入的文件是从 1 到 3000 的数字。我很好地阅读了该文件并将其存储在一个名为numbers
. 出于某种原因,当我写入文件时,它正确写入了前 2913 行,但这是输出的结尾:
2908
2909
2910
2911
2912
2913
2
每次我运行程序时,该文件都会在数字 2914 的中间结束。该程序不会抛出异常。为什么会出现这种行为?
这是我的代码:
Sub Main() As Integer
Dim numbers As New List(Of String)
ReadFile(numbers, "C:\test.txt")
WriteFile(numbers, "C:\test2.txt")
End Function
Private Sub ReadFile(ByRef lines As List(Of String), _
ByVal filePath As String)
Dim sr As New System.IO.StreamReader(filePath)
Do While sr.Peek <> -1
lines.Add(sr.ReadLine)
Loop
End Sub
Private Sub WriteFile(ByVal lines As List(Of String), _
ByVal filePath As String)
Dim sw As New System.IO.StreamWriter(filePath, False)
For Each line In lines
sw.WriteLine(line)
Next
End Sub