文本处理之-打开文件、保存文件
vb.net提供了3种文本文件打开和保存的方法,FileOpen函数、StreamReader类和My命名空间。《Visual.Basic2008 step by Step》书中只提出了这三种方式如何打开文件和FileOpen函数保存文件,StreamReader类和My命名空间方式我也一同整理出来记在这里。
FileOpen函数打开和保存文件实例
Private Sub 打开文件OToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开文件OToolStripMenuItem.Click
Dim AllText As String = "", LineOfText As String = ""
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt" '设置打开文件类型
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Try '使用错误处理程序捕获错误
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
Do Until EOF(1) '从文件中读取文本行
LineOfText = LineInput(1)
'逐行将文本添加到alltext变量中
AllText = AllText & LineOfText & vbCrLf
Loop
TextBox1.Text = AllText
Catch ex As Exception
MsgBox("文件打开错误")
Finally
FileClose(1)
End Try
End If
End Sub
Private Sub 保存SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存SToolStripMenuItem.Click
SaveFileDialog1.Filter = "Text files (*.txt) | *.txt" '设置保存类型
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
PrintLine(1, TextBox1.Text)
FileClose(1)
End If
End Sub
StreamReader类打开和保存文件实例
Imports System
Imports System.IO
Imports System.Text.Encoding '字符空间
Private Sub 打开文件ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开文件ToolStripMenuItem.Click
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt" '设置打开文件类型
Dim StreamtoDisplay As StreamReader
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
StreamtoDisplay = New StreamReader(OpenFileDialog1.FileName, [Default]) '设置字符为系统默认字符,否则在处理中文时会出现乱码
TextBox1.Text = StreamtoDisplay.ReadToEnd
StreamtoDisplay.Close()
TextBox1.Select(0, 0)
End If
End Sub
Private Sub 保存ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存ToolStripMenuItem.Click
SaveFileDialog1.Filter = "Text files (*.txt) | *.txt" '设置保存类型
Dim myStreamWriter As StreamWriter = New StreamWriter(SaveFileDialog1.FileName)
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
myStreamWriter.Write(TextBox1.Text)
myStreamWriter.Close()
End If
End Sub
My命名空间打开和保存文件实例
Imports System.Text.Encoding '字符空间
Private Sub 打开文件MycomputerfilesystemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开文件MycomputerfilesystemToolStripMenuItem.Click
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt" '设置打开文件类型
Dim AllText As String = ""
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
AllText = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName, [Default]) '设置字符为系统默认字符,否则在处理中文时会出现乱码
TextBox1.Text = AllText
End If
End Sub
Private Sub 保存MyComputerFileSystemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存MyComputerFileSystemToolStripMenuItem.Click
SaveFileDialog1.Filter = "Text files (*.txt) | *.txt"
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False) 'False参数,如果文件存在则覆盖文件,Ture参数,追加本文到文件
End If
End Sub