從文字檔案讀取和寫入文字檔案



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

從輸入流讀取下一個字元,並將字元位置前進一個字元。

示例

以下示例演示瞭如何讀取名為 Jamaica.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
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

猜猜編譯並執行程式時會顯示什麼!

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 fileProg
   Sub Main()
      Dim names As String() = New String() {"Zara Ali", _
         "Nuha Ali", "Amir Sohel", "M 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

編譯並執行上述程式碼後,將產生以下結果:

Zara Ali
Nuha Ali
Amir Sohel
M Amlan 
vb.net_file_handling.htm
廣告

© . All rights reserved.