vb.net读写文件 vb 文件读写( 二 )


' (例如,单词“Hello”作为整数?),所以我们必须捕捉写入
' 错误,并通知用户未能执行该任务
Try
Select Case selection
Case "Boolean"
Dim b As Boolean = Convert.ToBoolean(output)
w.Write( b )
Case "String"
Dim s As String = Convert.ToString(output)
w.Write( s )
Case "Integer"
Dim i As Int32 = Convert.ToInt32(output)
w.Write(i)
End Select
Catch E As Exception
' 让用户知道未能写入该信息
strOutput = "写异常:"chr(13)_
"无法以所请求的格式写入要写入的信息 。"_
chr(13)"请输入尝试写入的数据类型的有效值"
End Try
fs.Close()
return strOutput
End Function
End Class
Sub btnAction_Click(src As Object, E As EventArgs)
Dim s As String = ""
' 写出文件
s = TestBinary.WriteFile(txtInput.Text, lstDataIn.SelectedItem.Text)
If s = "" Then
Try
' 读回信息,显示信息...
txtOutput.Text = TestBinary.ReadFile(lstDataIn.SelectedItem.Text)
Catch Exc As Exception
' 让用户知道未能写入信息
s = "读异常:"chr(13)_
"无法以所请求的格式读取要写入的信息 。"_
chr(13)"请输入尝试写入的数据类型的有效值"
End Try
Else
txtOutput.Text = s
End If
End Sub
/script
html
head
link rel="stylesheet" href="https://www.04ip.com/post/intro.css"
/head
body style="background-color:f6e4c6"
form method=post runat="server"
p
table
tr
tdb
下面的示例使用 BinaryWriter 对象创建一个二进制文件,然后使用 BinaryReader 读取该信息 。/b可以选择不同的对象来将所需的信息写入文件
此演示用于强调您需要知道如何读取已写入的二进制文件 。一旦以某种格式写入数据,就只能以该格式读取该信息 。但是,可以将多种不同的数据类型写入文件 。在此演示中,输入任意字符串并将它们作为字符串读?。?对于整型 , 仅输入整型数值项(试试浮点数字,然后看看会发生什么...);对于布尔型项,仅输入词“false”和“true” 。
p
hr
/td
/tr
/table
asp:Table id="basetable" runat="server" border="0" cellspacing="0" cellpadding="5"
asp:tablerow
asp:tablecell verticalalign="top"
请选择要保存到二进制文件的数据类型...
/asp:tablecell
asp:tablecell verticalalign="top"
asp:listbox id="lstDataIn" runat="server"
asp:listitemBoolean/asp:listitem
asp:listitem selected="true"String/asp:listitem
【vb.net读写文件 vb 文件读写】asp:listitemInteger/asp:listitem
/asp:listbox
/asp:tablecell
asp:tablecell verticalalign="top"
asp:button id="btnAction" onclick="btnAction_Click" Text="写入/读取文件" runat="server"/
/asp:tablecell
/asp:tablerow
请教在VB.net中如何将数据写入txt文件、再从txt文件读出?软糖来告诉你吧 。
VB.net中读写文件主要使用System.IO命名空间 。
① 使用 File.ReadAllText 读取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
② 使用 StreamReader 读取vb.net读写文件,注意编码格式和写入vb.net读写文件的编码保持一致 。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd()
sr.Close()
③ 使用 File.WriteAllText 写入 , 会覆盖同名vb.net读写文件的文件 。
Dim 要写的内容 As String = ""
File.WriteAllText(文件路径, 要写的内容, System.Text.Encoding.UTF8)
④ 使用 StreamWriter 写入 。
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False,System.Text.Encoding.UTF8)

推荐阅读