Visual Basic 读取和写入文本

StreamReaderStreamWriter 类用于从文本文件读取数据和将数据写入文本文件。这些类继承自抽象基类 Stream,抽象基类流支持将字节读写到文件流中。


StreamReader 类

StreamReader 类还继承自抽象基类 TextReader,该类表示用于读取一系列字符的读取器。

下表介绍了 StreamReader 类的一些常用方法:

编号方法名称 & 用途
1

Public Overrides Sub Close

关闭 StreamReader 对象和底层流,并释放与 StreamReader 关联的所有系统资源。

2

Public Overrides Function Peek As Integer

返回下一个可用字符,但不使用它。

3

Public Overrides Function Read As Integer

从输入流中读取下一个字符,并将字符位置前进一个字符。


实例

下面的实例演示如何读取名为 Jamaica.txt 的文本文件:

Jamaica.txt

  1. Down the way where the nights are gay
  2. And the sun shines daily on the mountain top
  3. I took a trip on a sailing ship
  4. And when I reached Jamaica
  5. I made a stop
实例
  1. Imports System.IO
  2. Module fileProg
  3. Sub Main()
  4. Try
  5. ' Create an instance of StreamReader to read from a file.
  6. ' The using statement also closes the StreamReader.
  7. Using sr As StreamReader = New StreamReader("e:/jamaica.txt")
  8. Dim line As String
  9. ' Read and display lines from the file until the end of
  10. ' the file is reached.
  11. line = sr.ReadLine()
  12. While (line <> Nothing)
  13. Console.WriteLine(line)
  14. line = sr.ReadLine()
  15. End While
  16. End Using
  17. Catch e As Exception
  18. ' Let the user know what went wrong.
  19. Console.WriteLine("The file could not be read:")
  20. Console.WriteLine(e.Message)
  21. End Try
  22. Console.ReadKey()
  23. End Sub
  24. End Module

结果如下:

  1. Down the way where the nights are gay
  2. And the sun shines daily on the mountain top
  3. I took a trip on a sailing ship
  4. And when I reached Jamaica
  5. 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 类将文本数据写入文件中:

  1. Imports System.IO
  2. Module fileProg
  3. Sub Main()
  4. Dim names As String() = New String() {"Zara Ali", _
  5. "Nuha Ali", "Amir Sohel", "M Amlan"}
  6. Dim s As String
  7. Using sw As StreamWriter = New StreamWriter("names.txt")
  8. For Each s In names
  9. sw.WriteLine(s)
  10. Next s
  11. End Using
  12. ' Read and show each line from the file.
  13. Dim line As String
  14. Using sr As StreamReader = New StreamReader("names.txt")
  15. line = sr.ReadLine()
  16. While (line <> Nothing)
  17. Console.WriteLine(line)
  18. line = sr.ReadLine()
  19. End While
  20. End Using
  21. Console.ReadKey()
  22. End Sub
  23. End Module

结果如下:

  1. Hakou Ali
  2. Nacy Lee
  3. Amir Wong
  4. Marry Amlan

分类导航