VB.NET 读写HTML 文件,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

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

示例

以下示例演示如何读取名为 test.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

文件:fileProg.vb –

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

编译和运行程序时显示的结果如下 –

F:workspvb.netfilehandle>vbc fileProg.vb Microsoft (R) Visual Basic Compiler version 14.0.1038 for Visual Basic 2012 Copyright (c) Microsoft Corporation. All rights reserved. ... ... F:workspvb.netfilehandle>fileProg.exe 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

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 writeFile Sub Main() Dim names As String() = New String() {"Hakou Ali", "Nacy Lee", "Amir Wong", "Marry 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

执行上面示例代码,得到以下结果 –

F:workspvb.netfilehandle>vbc writeFile.vb Microsoft (R) Visual Basic Compiler version 14.0.1038 for Visual Basic 2012 Copyright (c) Microsoft Corporation. All rights reserved. ...... F:workspvb.netfilehandle>writeFile.exe Hakou Ali Nacy Lee Amir Wong Marry Amlan

¥ 我要打赏   纠错/补充 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值