
正文
vb.net重新写文件 vbnet writeline
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
关于VB.NET写文件的问题
Dim sr As New IO.StreamReader("路径") '打开文件
Dim str As String = sr.ReadToEnd() '读取文本
sr.Close() '关闭流
str = "not" str '修改文字
Dim sw As New IO.StreamWriter("路径") '打开
sw.Write(str) '写入
sw.Close() '关闭
相关问答
Q1: 请教在VB.net中如何将数据写入txt文件、再从txt文件读出?
软糖来告诉你吧。
VB.net中读写文件主要使用System.IO命名空间。
① 使用 File.ReadAllText 读取
Dim s As String = System.IO.File.ReadAllText("C:\a.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。
◆ 满意请采纳,谢谢 ◆
Q2: vb.net怎么往已经建立好的dat文件里写东西,还不覆盖原本的数据?
如果想继续编辑之前的文档,在TXT文件尾部继续添加文本,那么还需要在函数后边加个参数。
VB 代码
方法1:
Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt")
sw.Write("abc" vbCrLf)
sw.Close()
Dim sw2 As StreamWriter = New StreamWriter("C:\temp\test.txt", True)
sw2.Write("456" vbCrLf)
sw2.Close()
方法2:
My.Computer.FileSystem.WriteAllText("test.txt", "This is test Text", True)
方法3:
System.IO.File.AppendAllText("c:\temp\test.txt", "this is extra test file")
详见:“网页链接”
关于vb.net重新写文件和vbnet writeline的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







