- VB.Net基礎教程
- VB.Net - 首頁
- VB.Net - 概述
- VB.Net - 環境設定
- VB.Net - 程式結構
- VB.Net - 基本語法
- VB.Net - 資料型別
- VB.Net - 變數
- VB.Net - 常量
- VB.Net - 修飾符
- VB.Net - 語句
- VB.Net - 指令
- VB.Net - 運算子
- VB.Net - 決策制定
- VB.Net - 迴圈
- VB.Net - 字串
- VB.Net - 日期和時間
- VB.Net - 陣列
- VB.Net - 集合
- VB.Net - 函式
- VB.Net - 子程式
- VB.Net - 類和物件
- VB.Net - 異常處理
- VB.Net - 檔案處理
- VB.Net - 基本控制元件
- VB.Net - 對話方塊
- VB.Net - 高階窗體
- VB.Net - 事件處理
- VB.Net高階教程
- VB.Net - 正則表示式
- VB.Net - 資料庫訪問
- VB.Net - Excel表格
- VB.Net - 傳送電子郵件
- VB.Net - XML處理
- VB.Net - Web程式設計
- VB.Net有用資源
- VB.Net - 快速指南
- VB.Net - 有用資源
- VB.Net - 討論
從文字檔案讀取和寫入文字檔案
StreamReader 和 StreamWriter 類用於從文字檔案讀取資料和向文字檔案寫入資料。這些類繼承自抽象基類 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