vb.net中读取txt的简单介绍

VB.NET读取TXT文件数据保存为数组VB.NET编程读取txt文本文档中的数据 , 并把数据保存为数组 , 代码如下:
'写配件文件
Private Sub saveIni(ByVal filePath As String, ByVal str As String)
Dim sw As StreamWriter = New StreamWriter(filePath, True) 'true是指以追加的方式打开指定文件
sw.WriteLine(str)
sw.Flush()
sw.Close()
sw = Nothing
End Sub
'读配件文件
Private Function readIni(ByVal filePath As String)
Dim iniDt As New DataTable
iniDt.Columns.Add("text")
iniDt.Columns.Add("value")
Try
Dim sr As StreamReader = New StreamReader(filePath, System.Text.Encoding.Default)
Dim line As String = ""
While Not sr.EndOfStream
Dim str = sr.ReadLine()'读取当前行
iniDt.Rows.Add(New String() {
str(0),
str(1)
})
End While
sr.Close()
sr = Nothing
Catch ex As Exception
End Try
Return iniDt
End Function
如何用vb.net编写读取txt内容的代码?窗体上添加2个文本框vb.net中读取txt,设置成多行vb.net中读取txt,2个按钮 , 在文本框1里随便输入若干文字,可以多行,单击按钮1,保存到文件 。然后单击按钮2 , 把刚才写入vb.net中读取txt的文件读到文本框2里 。
代码如下vb.net中读取txt:
'写文本文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建(写入)一个文本文件
Dim MyStream As New System.IO.FileStream(Application.StartupPath"\Ssk.txt", System.IO.FileMode.Create)
Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.Default)
MyWriter.WriteLine(TextBox1.Text)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
End Sub
'读文本文件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'读取一个文本文件
Dim MyReader As New System.IO.StreamReader(Application.StartupPath"\Ssk.txt", System.Text.Encoding.UTF8)
TextBox2.Text = MyReader.ReadToEnd()
MyReader.Close()
End Sub
气斜射入水或其vb.net中读取txt他介质 , 折射光线与入射光线法线在
请教在VB.net中如何将数据写入txt文件、再从txt文件读出?软糖来告诉你吧 。
VB.net中读写文件主要使用System.IO命名空间 。
① 使用 File.ReadAllText 读取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
【vb.net中读取txt的简单介绍】② 使用 StreamReader 读?。?注意编码格式和写入的编码保持一致 。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd()
sr.Close()
③ 使用 File.WriteAllText 写入,会覆盖同名的文件 。
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)
sw.WriteLine(TextTB.Text)
sw.Close()
⑤ 使用 StreamWriter 追加写入 。
将上面代码的第二个参数False改为True 。
◆ 满意请采纳 , 谢谢 ◆
vb.net文件读取txt1、实现上传按钮方法代码 。
2、判断图片对象是否为空代码 。
3、取得数据库字段 dt.Rows(0)("Pic")方法代码 。
4、字节数组转换为Image类型方法代码 。
5、处理SQL中操作Image类型方法代码 。
6、实现的上传结果 。
VB.net窗体设计中 , 如何读取.txt文件中的数据?1、新建一个标准vb.net中读取txt的VB EXE工程vb.net中读取txt , 只有一个Formvb.net中读取txt,Form上有两个按钮vb.net中读取txt:Command1和Command2 。
2、双击Command1添加如下代码
Private Sub Command1_Click()
Dim strFileAs String
Dim intFileAs Integer
Dim strDataAs String
strFile = "c:\学生成绩.txt"
intFile = FreeFile
Open strFile For Input As intFile
strData = https://www.04ip.com/post/StrConv(InputB(FileLen(strFile), intFile), vbUnicode)
Debug.Print strData
Close intFile
End Sub
3、按F8开始单步调试代码,点击Command1,进入单步调试功能,
4、多次按下F8或直接按下F5运行完成 , 就完成vb.net中读取txt了读取文本文件内容并输出到立即窗口 。
VB.net 如果读取txt数据(或十进制dat数据)vb.net虽也有input语句,但一次只能读取到一个变量中,可以用TextFieldParser类代替,但似乎没以前vb.net中读取txt的方便 。不过比以前vb.net中读取txt的更灵活 。写入文件Write还是可以用,在Microsoft.VisualBasic.FileIO中 。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileName As String = "E:\User Documents\Master\My Documents\电子阅读\股票\table2.csv"
Using Recrods As New Microsoft.VisualBasic.FileIO.TextFieldParser(fileName) '建立TextFieldParser对象
'MyReader.TextFieldType = FieldType.Delimited
Recrods.SetDelimiters(",") '把字段分隔符设置为","
Dim curRow() As String
Do Until Recrods.EndOfData
curRow = Recrods.ReadFields() '读取记录行,返回字符串数组,所以不同字段类型需要自己转换 。
Debug.Print(Join(curRow, vbTab))
Loop
End Using
End Sub
vb.net中读取txt的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、vb.net中读取txt的信息别忘了在本站进行查找喔 。

    推荐阅读