课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

从文本文件读取和写入

当前位置:免费教程 » 程序设计 » VB.Net

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


StreamReader类

StreamReader类还继承自抽象基类TextReader,它代表用于阅读一系列字符的读者。 下表描述了StreamReader类的一些常用方法:


S.N方法名称和用途
1

Public Overrides Sub Close

It closes the StreamReader object and the underlying stream and releases any system resources associated with the reader.

它关闭StreamReader对象和基础流,并释放与读取器相关联的任何系统资源。

2

Public Overrides Function Peek As Integer

Returns the next available character but does not consume it.

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

3

Public Overrides Function Read As Integer

Reads the next character from the input stream and advances the character position by one character.

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


示例:

以下示例演示读取名为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
猜测当你编译和运行程序时显示什么?

StreamWriter类

StreamWriter类继承自抽象类TextWriter,表示一个写入器,它可以写一系列字符。

下表显示了此类最常用的一些方法:
S.N方法名称和用途
1Public Overrides Sub Close 
Closes the current StreamWriter object and the underlying stream.
关闭当前StreamWriter对象和基础流。
2

Public Overrides Sub Flush

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

清除当前写入程序的所有缓冲区,并使任何缓冲的数据写入基础流。

3

Public Overridable Sub Write (value As Boolean)

Writes the text representation of a Boolean value to the text string or stream. (Inherited from TextWriter.)

将布尔值的文本表示写入文本字符串或流。(从TextWriter继承。)

4

Public Overrides Sub Write (value As Char)

Writes a character to the stream.

将字符写入流。

5

Public Overridable Sub Write (value As Decimal)

Writes the text representation of a decimal value to the text string or stream.

将十进制值的文本表示写入文本字符串或流。

6

Public Overridable Sub Write (value As Double)

Writes the text representation of an 8-byte floating-point value to the text string or stream.

将8字节浮点值的文本表示写入文本字符串或流。

7

Public Overridable Sub Write (value As Integer)

Writes the text representation of a 4-byte signed integer to the text string or stream.

将4字节有符号整数的文本表示写入文本字符串或流。

8

Public Overrides Sub Write (value As String)

Writes a string to the stream.

将字符串写入流。

9

Public Overridable Sub WriteLine

Writes a line terminator to the text string or stream.

将行终止符写入文本字符串或流。

以上列表并不详尽。 有关方法的完整列表,请访问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. Zara Ali
  2. Nuha Ali
  3. Amir Sohel
  4. M Amlan
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号