vb.net 读写html 文件路径,VB.Net - 从文本文件读取和写入

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

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

S.N方法名称和用途

1Public Overrides Sub Close

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

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

2Public Overrides Function Peek As Integer

Returns the next available character but does not consume it.

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

3Public Overrides Function Read As Integer

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

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

示例:

以下示例演示读取名为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 stopImports 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,表示一个写入器,它可以写一系列字符。

下表显示了此类最常用的一些方法:

S.N方法名称和用途

1Public Overrides Sub Close

Closes the current StreamWriter object and the underlying stream.

关闭当前StreamWriter对象和基础流。

2Public Overrides Sub Flush

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

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

3Public Overridable Sub Write (value As Boolean)

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

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

4Public Overrides Sub Write (value As Char)

Writes a character to the stream.

将字符写入流。

5Public Overridable Sub Write (value As Decimal)

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

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

6Public Overridable Sub Write (value As Double)

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

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

7Public Overridable Sub Write (value As Integer)

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

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

8Public Overrides Sub Write (value As String)

Writes a string to the stream.

将字符串写入流。

9Public Overridable Sub WriteLine

Writes a line terminator to the text string or stream.

将行终止符写入文本字符串或流。以上列表并不详尽。 有关方法的完整列表,请访问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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值