VB.Net讀取和寫入文字檔案

2019-10-16 23:02:02

StreamReaderStreamWriter類用於讀取和寫入資料到文字檔案。這些類繼承自抽象基礎類別Stream,它支援讀取和寫入位元組到檔案流中。

StreamReader類

StreamReader類也從抽象基礎類別TextReader繼承,它代表讀取一系列字元的讀取器。下表介紹了StreamReader類的一些常用方法:

編號 方法 描述
1 Public Overrides Sub Close 它關閉StreamReader物件和底層流,並釋放與閱讀器相關的所有系統資源。
2 Public Overrides Function Peek As Integer 返回下一個可用字元,但不會消耗它。
3 Public Overrides Function Read As Integer 從輸入流中讀取下一個字元,並將字元位置前進一個字元。

範例

以下範例演示如何讀取名為 test.txt 的文字檔案。該檔案讀取:

Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop

檔案:fileProg.vb -

Imports System.IO
Module fileProg
   Sub Main()
      Try
          ' Create an instance of StreamReader to read from a file. '
          ' The using statement also closes the StreamReader. '
          Using sr As StreamReader = New StreamReader("e:/jamaica.txt")
              Dim line As String
              ' Read and display lines from the file until the end of  '
              ' the file is reached. '
              line = sr.ReadLine()
              While (line <> Nothing)
                  Console.WriteLine(line)
                  line = sr.ReadLine()
              End While
          End Using
      Catch e As Exception
          ' Let the user know what went wrong.'
          Console.WriteLine("The file could not be read:")
          Console.WriteLine(e.Message)
      End Try
      Console.ReadKey()
   End Sub
End Module

編譯和執行程式時顯示的結果如下 -

F:\worksp\vb.net\filehandle>vbc fileProg.vb
Microsoft (R) Visual Basic Compiler version 14.0.1038
for Visual Basic 2012
Copyright (c) Microsoft Corporation.  All rights reserved.
... ...
F:\worksp\vb.net\filehandle>fileProg.exe
Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop

StreamWriter類

StreamWriter類繼承自抽象類TextWriter,它表示作者,可以編寫一系列字元。

下表顯示了該類最常用的一些方法:

編號 方法 描述
1 Public Overrides Sub Close 關閉當前StreamWriter物件和基礎流。
2 Public Overrides Sub Flush 清除當前寫入器的所有緩衝區,並將所有緩衝的資料寫入基礎流。
3 Public Overridable Sub Write (value As Boolean) 將布林值的文字表示形式寫入文字字串或流(從TextWriter繼承。)
4 Public Overrides Sub Write (value As Char) 將一個字元寫入流中。
5 Public Overridable Sub Write (value As Decimal) 將十進位制值的文字表示形式寫入文字字串或流。
6 Public Overridable Sub Write (value As Double) 8位元組浮點值的文字表示形式寫入文字字串或流。
7 Public Overridable Sub Write (value As Integer) 4位元組有符號整數的文字表示形式寫入文字字串或流。
8 Public Overrides Sub Write (value As String) 將一個字串寫入流。
9 Public Overridable Sub WriteLine 寫一個行結束符到文字字串或流。

以上列表並不詳盡。 有關完整的方法列表,請存取Microsoft的文件

範例

以下範例演示如何使用StreamWriter類將文字資料寫入檔案中:

Imports System.IO
Module writeFile
   Sub Main()
      Dim names As String() = New String() {"Hakou Ali", "Nacy Lee", "Amir Wong", "Marry Amlan"}
      Dim s As String
      Using sw As StreamWriter = New StreamWriter("names.txt")
          For Each s In names
              sw.WriteLine(s)
          Next s
      End Using
      ' Read and show each line from the file. '
      Dim line As String
      Using sr As StreamReader = New StreamReader("names.txt")
          line = sr.ReadLine()
          While (line <> Nothing)
              Console.WriteLine(line)
              line = sr.ReadLine()
          End While
      End Using
      Console.ReadKey()
   End Sub
End Module

執行上面範例程式碼,得到以下結果 -

F:\worksp\vb.net\filehandle>vbc writeFile.vb
Microsoft (R) Visual Basic Compiler version 14.0.1038
for Visual Basic 2012
Copyright (c) Microsoft Corporation.  All rights reserved.
......
F:\worksp\vb.net\filehandle>writeFile.exe
Hakou Ali
Nacy Lee
Amir Wong
Marry Amlan