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

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

VB.NET 读写HTML 文件,VB.Net读取和写入文本文件相关推荐

  1. vb直接读写文件服务器文件,vb使用open方法读写文件

    vb使用open方法读写文件 vb使用open方法读写文件 (一)打开和关闭文件 1.顺序文件 打开顺序文件,我们可以使用Open语句.它的格式如下: Open pathname For [Input ...

  2. VB.NET 读写HTML 文件,VB.NET 简单介绍文件的读写----流类

    我们知道在计算机内部处理的数据信息以及存放在在磁盘上的文件,对于计算机而言就是一堆二进制数(字节).这些字节(甚至是位,但计算机通常按字节对它们进行分组).只有在程序解析了这些字节时,它们才具有了对用 ...

  3. vb调用本地html,在VB中调用HTMLHELP文件VB -电脑资料

    在 VB 中调用HTMLHELP文件 湖北省襄樊市劳动保险处 闫东 ---- HTML帮助文件是 WINDOWS 基本帮助系统的一种新标准,在WINDOWS 98中我们到处都可以看到它的身影, 在VB ...

  4. 【python图像处理】txt文件数据的读取与写入

    在使用python进行数据和图像处理的过程中,经常会遇到从txt文件中读取数据.已经将处理过程中的矩阵数据写入到txt文件的情形,如在伪彩映射中读取颜色映射表. 下面介绍几种我平时常用的txt文件数据 ...

  5. android studio 写文件,在Android Studio中从我自己的类读取和写入文本文件

    我一直在试图创建一个名为TextFileReaderWriter的类我想使用getters和setters来读取和写入文本文件,以便我可以调用类,并通过简单地使用setfileContents(som ...

  6. 【MATLAB】读取和写入文本文件

    在MATLAB中,来读取和写入文本文件是很简单的事.下面,就来简单介绍下.如果有其他问题,请留言. 一.读取文本文件 思路: 1.用fopen来打开一个文件句柄 2.用fgetl来获得文件中的一行,如 ...

  7. python在读写文件之前需要创建文件对象-python读取或写入文件

    一.创建并读取文本文件 1.该方法需要关闭filereader对象 #!/usr/bin/env python3#读取文件 input_file = "F://python入门//文件//一 ...

  8. python3读写excel文件_python3 循环读取excel文件并写入json操作

    文件内容: excel内容: 代码: import xlrd import json import operator def read_xlsx(filename): # 打开excel文件 data ...

  9. 怎么利用迭代器写入mysql_流迭代器实现文件操作(读取和写入)方法详解

    流迭代器并不知道底层流的特性.当然,它们只适用于文本模式,否则它们不会关心数据是什么.流迭代器可以以文本模式来读写任何类型的流.这意味着除了其他的一些流之外,我们可以用迭代器以文本模式来读和写文件.在 ...

最新文章

  1. 2021年大数据ELK(二十五):添加Elasticsearch数据源
  2. 见光死怎么办?如何提升用户对网站的好感,提升转化?
  3. docker 启动 springboot 项目
  4. python爬取的信息条数比页面显示多_Python爬取分析北京二手房数据?数据结果真的太吓人了...
  5. 二维傅里叶变换是怎么进行的?
  6. 最IN的云原生架构,阿里云 Serverless 事件总线 EventBridge 重磅发布
  7. ASP.NET路由系统实现原理:HttpHandler的动态映射
  8. 从开源爱好者到 Apache 董事,他花了 11 年
  9. Java思维导图(七)
  10. log4j的详细配置(最省心完美配置)
  11. JAVA企业微信事件接收
  12. Linux 用户的手边工具:Guide to Linux
  13. MATLAB各个产品概述----哪些产品需要安装?哪些产品不需要安装?阅完了然
  14. Jupyter Notebook 快捷键、安装主题、修改颜色、设置字体、注释斜体修改
  15. 万能python,画个滑稽来玩玩
  16. Hadoop 启动 HDFS DataNode 时报错:Invalid dfs.datanode.data.dir
  17. strtol strtoll strtoul strtoull应用
  18. 有效的括号(leetcode 20)
  19. golang gopher-lua 遍历table元素
  20. 机电开关的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告

热门文章

  1. 直接拿来用!最火的Android开源项目(二)
  2. 比较婴儿fNIRS数据的不同预处理流程
  3. iOS开发之fastlane自动化打包工具安装和使用
  4. 熔盐储能系统市场调研
  5. 《Building Services Engineering Research Technology》期刊介绍(SCI 4区)
  6. 和ha水解的不同_水解蛋白奶粉有哪些,让超启能恩缓解宝宝的“小敏感”
  7. pycharm如何全局搜索
  8. centos安装xfce
  9. java 用折半查找,java折半查找算法
  10. ionic+Angular 手机二维码扫描