Tester.txt
它只是说“你好” 。
我现在运行这个:
Sub Main()
Dim fileName As String
Dim aFileStream As FileStream
Dim aWriter As StreamWriter
fileName = "...\Tester.txt"
Try
aFileStream = New FileStream(fileName, FileMode.OpenOrCreate)
aWriter = New StreamWriter(aFileStream)
aWriter.WriteLine("World")
Catch ex As Exception
MessageBox.Show("found this {0}", ex.Message)
Finally
If aWriter IsNot Nothing Then
aWriter.Close()
End If
If aFileStream IsNot Nothing Then
aFileStream.Close()
End If
End Try
End Sub
现在“Hello”已经被“World”覆盖了。
我想保留文件中的现有文本,所以我想我可以先将该文本读入一个变量,然后再编写新内容:
Sub Main()
Dim fileName As String
Dim aFileStream As FileStream
Dim aWriter As StreamWriter
Dim aReader As StreamReader
fileName = "...\Tester.txt"
Try
aFileStream = New FileStream(fileName, FileMode.OpenOrCreate)
aReader = New StreamReader(aFileStream)
Dim myContent As String = aReader.ReadToEnd()
aWriter = New StreamWriter(aFileStream)
aWriter.WriteLine("World")
Catch ex As Exception
MessageBox.Show("found this {0}", ex.Message)
Finally
If aWriter IsNot Nothing Then
aWriter.Close()
End If
If aReader IsNot Nothing Then
aReader.Close()
End If
If aFileStream IsNot Nothing Then
aFileStream.Close()
End If
End Try
End Sub
这是保留文件中已有文本的标准方法吗?
我可以在不使用实例的情况下保留现有信息StreamReader
吗?