vb.net2005中提示无法在流的结尾之外进行读取
我给你一个实例,你自己修改;
一,运行界面:
二,完整代码:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'写文件
Dim Myw As New FileStream(Application.StartupPath & "\实验文件.txt", FileMode.Create)
Dim MyB_Write As BinaryWriter = New BinaryWriter(Myw)
MyB_Write.Write(TextBox1.Text)
Myw.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'读文件
Dim Myr As New FileStream(Application.StartupPath & "\实验文件.txt", FileMode.Open, FileAccess.Read)
Myr.Position = 0
Dim MyB_Read As New BinaryReader(Myr)
Dim MyFileLength As Integer = CInt(Myr.Length - Myr.Position) - 1
Dim MyFileData(MyFileLength) As Char
MyB_Read.Read(MyFileData, 0, MyFileLength)
Myr.Close()
TextBox2.Text = ""
Dim i As Integer
For i = LBound(MyFileData) To UBound(MyFileData)
TextBox2.Text = TextBox2.Text & MyFileData(i)
Next
End Sub
End Class