将数据写入文本文件,然后尝试从文本文件中读取数据显示流不可读
writing data to text file and then trying to read the data from the text file shows stream not readable
我创建了一个表单来将数据写入文本文件并从文本文件中读取数据。如果我 运行 程序并将数据写入文本文件,程序运行良好。数据被保存到文件中。然后我停止程序并 re-run 它现在从文件中读取数据,文本数据也显示在相应的文本框中。
但是,当我将数据写入文本文件,然后在没有 re-run 程序的情况下尝试从文本文件中读取数据时,问题就出现了。现在在这种情况下,它显示错误:Stream was not readable.
为什么会这样?
Imports System.IO
Public Class Form1
Dim fileStream_ As FileStream
Dim fileWrite_ As StreamWriter
Dim fileRead_ As StreamReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
fileStream_ = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
' for WRITE data to text file
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
fileWrite_ = New StreamWriter(fileStream_)
fileWrite_.Write(TextBox1.Text)
fileWrite_.Close()
MsgBox("Text written to file", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
' for READ data from text file
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
fileRead_ = New StreamReader(fileStream_)
TextBox2.Text = fileRead_.ReadToEnd
fileRead_.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class
当你关闭 writer 的同时,底层的 steam 也会被关闭,如果你需要从同一个文件中读取,你需要创建一个新的流实例。
参考:https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter.close?view=net-6.0
Imports System.IO
Public Class Form1
Dim fileStream_ As FileStream
Dim fileWrite_ As StreamWriter
Dim fileRead_ As StreamReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
' for WRITE data to text file
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
fileStream_ = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.Write)
fileWrite_ = New StreamWriter(fileStream_)
fileWrite_.Write(TextBox1.Text)
fileWrite_.Close()
MsgBox("Text written to file", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
' for READ data from text file
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
fileStream_ = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.Read)
fileRead_ = New StreamReader(fileStream_)
TextBox2.Text = fileRead_.ReadToEnd
fileRead_.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class
我还建议您使用 'Using' 语句包装流,以便更好地管理资源
不要保留这些全局变量,因为您并不真正需要它们。
当由于某种原因它们不再有效时,它们可以向您射击。
您的示例是全局变量引入了细微且不明显的错误的示例。当您关闭 StreamWriter 时,fileStream_ 变量会自动处理,并且在下一个按钮单击时不再可用。
当然,如果您 close/reopen 应用程序并直接读取文件,它就可以工作。但是,这将是写入失败。
而是使用 Using statement 来正确处理所有一次性对象,如 StreamWriter、StreamReader 或 FileStream
因此您的点击处理程序可以重写为
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Using fileStream_ As FileStream = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Using fileWrite_ As StreamWriter = New StreamWriter(fileStream_)
fileWrite_.Write(TextBox1.Text)
' the call to close is no more needed when inside the using
' fileWrite_.Close()
MsgBox("Text written to file", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Using fileStream_ As FileStream = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Using fileRead_ As StreamReader = New StreamReader(fileStream_)
TextBox2.Text = fileRead_.ReadToEnd
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
但是,除非您有正当理由让这些流存在,否则您可以用一条语句编写所有代码。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
File.WriteAllText("C:\Users\USER\Desktop\demo\vb.txt", TextBox1.Text)
MsgBox("Text written to file", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
TextBox2.Text = File.ReadAllText("C:\Users\USER\Desktop\demo\vb.txt")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
当您需要处理文件时,File class 有许多有用的方法
我创建了一个表单来将数据写入文本文件并从文本文件中读取数据。如果我 运行 程序并将数据写入文本文件,程序运行良好。数据被保存到文件中。然后我停止程序并 re-run 它现在从文件中读取数据,文本数据也显示在相应的文本框中。
但是,当我将数据写入文本文件,然后在没有 re-run 程序的情况下尝试从文本文件中读取数据时,问题就出现了。现在在这种情况下,它显示错误:Stream was not readable.
为什么会这样?
Imports System.IO
Public Class Form1
Dim fileStream_ As FileStream
Dim fileWrite_ As StreamWriter
Dim fileRead_ As StreamReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
fileStream_ = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
' for WRITE data to text file
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
fileWrite_ = New StreamWriter(fileStream_)
fileWrite_.Write(TextBox1.Text)
fileWrite_.Close()
MsgBox("Text written to file", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
' for READ data from text file
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
fileRead_ = New StreamReader(fileStream_)
TextBox2.Text = fileRead_.ReadToEnd
fileRead_.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class
当你关闭 writer 的同时,底层的 steam 也会被关闭,如果你需要从同一个文件中读取,你需要创建一个新的流实例。
参考:https://docs.microsoft.com/en-us/dotnet/api/system.io.streamwriter.close?view=net-6.0
Imports System.IO
Public Class Form1
Dim fileStream_ As FileStream
Dim fileWrite_ As StreamWriter
Dim fileRead_ As StreamReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
' for WRITE data to text file
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
fileStream_ = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.Write)
fileWrite_ = New StreamWriter(fileStream_)
fileWrite_.Write(TextBox1.Text)
fileWrite_.Close()
MsgBox("Text written to file", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
' for READ data from text file
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
fileStream_ = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.Read)
fileRead_ = New StreamReader(fileStream_)
TextBox2.Text = fileRead_.ReadToEnd
fileRead_.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class
我还建议您使用 'Using' 语句包装流,以便更好地管理资源
不要保留这些全局变量,因为您并不真正需要它们。
当由于某种原因它们不再有效时,它们可以向您射击。
您的示例是全局变量引入了细微且不明显的错误的示例。当您关闭 StreamWriter 时,fileStream_ 变量会自动处理,并且在下一个按钮单击时不再可用。
当然,如果您 close/reopen 应用程序并直接读取文件,它就可以工作。但是,这将是写入失败。
而是使用 Using statement 来正确处理所有一次性对象,如 StreamWriter、StreamReader 或 FileStream
因此您的点击处理程序可以重写为
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Using fileStream_ As FileStream = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Using fileWrite_ As StreamWriter = New StreamWriter(fileStream_)
fileWrite_.Write(TextBox1.Text)
' the call to close is no more needed when inside the using
' fileWrite_.Close()
MsgBox("Text written to file", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Using fileStream_ As FileStream = New FileStream("C:\Users\USER\Desktop\demo\vb.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Using fileRead_ As StreamReader = New StreamReader(fileStream_)
TextBox2.Text = fileRead_.ReadToEnd
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
但是,除非您有正当理由让这些流存在,否则您可以用一条语句编写所有代码。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
File.WriteAllText("C:\Users\USER\Desktop\demo\vb.txt", TextBox1.Text)
MsgBox("Text written to file", MsgBoxStyle.Information)
TextBox1.Text = ""
TextBox2.Text = ""
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
TextBox2.Text = File.ReadAllText("C:\Users\USER\Desktop\demo\vb.txt")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
当您需要处理文件时,File class 有许多有用的方法