当前位置: 首页 > news >正文

【.Net实用方法总结】 整理并总结System.IO中StringReader类及其方法介绍

🐋作者简介:博主是一位.Net开发者,同时也是RPA和低代码平台的践行者。
🐬个人主页:会敲键盘的肘子
🐰系列专栏:.Net实用方法总结
🦀专栏简介:博主针对.Net开发和C站问答过程中遇到的问题进行总结,形成本专栏,希望可以帮助到您解决问题。
🐶座右铭:总有一天你所坚持的会反过来拥抱你。


在这里插入图片描述

🌈写在前面:

本文主要介绍System.IO命名空间的StringReader 类,介绍其常用的方法和示例说明。


👉本文关键字:System.IO、StringReader类、方法示例、C#

文章目录

      • 1️⃣ System.IO命名空间
      • 2️⃣ StringReader类
        • ♈ 定义
        • ♉ 构造函数
          • 初始化从指定字符串进行读取的 StringReader 类的新实例。
        • ♌ 常用方法
          • Close() 关闭 StringReader
          • Dispose() 释放由 TextReader 对象使用的所有资源
          • Read(Char[], Int32, Int32) 读取输入字符串中的字符块,并将字符位置提升 count
          • ReadAsync(Char[], Int32, Int32) 异步从当前字符串中读取指定数目的字符并从指定索引开始将该数据写入缓冲区
          • ReadLine() 从当前字符串中读取一行字符并将数据作为字符串返回
          • ReadLineAsync() 从当前字符串中异步读取一行字符并将数据作为字符串返回
          • ReadToEnd() 读取从当前位置到字符串的结尾的所有字符并将它们作为单个字符串返回
          • ReadToEndAsync() 异步读取从当前位置到字符串的结尾的所有字符并将它们作为单个字符串返回
        • ♎ 更多方法

1️⃣ System.IO命名空间

.NET中的IO操作命名空间,包含允许读写文件数据流的类型以及提供基本文件和目录支持的类型。

我们在.NET中的IO操作,经常需要调用一下几个类。

  • FileStream类

​ 文件流类,负责大文件的拷贝,读写。

  • Path类

​ Path类中方法,基本都是对字符串(文件名)的操作,与实际文件没多大关系。

  • File类

    File类可以进行一些对小文件拷贝、剪切操作,还能读一些文档文件。

  • Dirctory类

    目录操作,创建文件、删除目录,获取目录下文件名等等。

2️⃣ StringReader类

♈ 定义

实现从字符串进行读取的 TextReader。

public class StringReader : System.IO.TextReader

示例

以下示例演示如何异步读取整个字符串。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadCharacters();
        }

        static async void ReadCharacters()
        {
            StringBuilder stringToRead = new StringBuilder();
            stringToRead.AppendLine("Characters in 1st line to read");
            stringToRead.AppendLine("and 2nd line");
            stringToRead.AppendLine("and the end");

            using (StringReader reader = new StringReader(stringToRead.ToString()))
            {
                string readText = await reader.ReadToEndAsync();
                Console.WriteLine(readText);
            }
        }
    }
}
// The example displays the following output:
//
// Characters in 1st line to read
// and 2nd line
// and the end
//

StringReader 使你可以同步或异步读取字符串。 可以使用或ReadAsync方法一次读取字符、一次Read使用ReadLine或方法的行以及使用ReadToEnd或ReadLineAsyncReadToEndAsync方法的整个字符串。

♉ 构造函数

初始化从指定字符串进行读取的 StringReader 类的新实例。
public StringReader (string s);

示例

此代码示例是为 StringReader 类提供的一个更大示例的一部分。

// From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
string aLine, aParagraph = null;
StringReader strReader = new StringReader(textReaderText);
while(true)
{
    aLine = strReader.ReadLine();
    if(aLine != null)
    {
        aParagraph = aParagraph + aLine + " ";
    }
    else
    {
        aParagraph = aParagraph + "\n";
        break;
    }
}
Console.WriteLine("Modified text:\n\n{0}", aParagraph);

♌ 常用方法

Close() 关闭 StringReader
public override void Close ();
Dispose() 释放由 TextReader 对象使用的所有资源
public void Dispose ();
Read(Char[], Int32, Int32) 读取输入字符串中的字符块,并将字符位置提升 count
public override int Read (char[] buffer, int index, int count);

参数

buffer

Char[]

当此方法返回时,包含指定的字符数组,此数组中 index 和 (index + count - 1) 之间的值被从当前源中读取的字符所替换。

index

Int32

buffer 中开始写入的位置。

count

Int32

最多读取的字符数。 如果在将指定数量的字符读入缓冲区之前就已达读取器的末尾,则返回该方法。

返回

Int32

已读取的字符数。 该数会小于或等于 count,具体取决于读取器中是否有可用的数据。 如果调用此方法时没有留下更多的字符供读取,则此方法返回 0(零)。

ReadAsync(Char[], Int32, Int32) 异步从当前字符串中读取指定数目的字符并从指定索引开始将该数据写入缓冲区
public override System.Threading.Tasks.Task<int> ReadAsync (char[] buffer, int index, int count);

参数

buffer

Char[]

当此方法返回时,包含指定的字符数组,此数组中 index 和 (index + count - 1) 之间的值被从当前源中读取的字符所替换。

index

Int32

buffer 中开始写入的位置。

count

Int32

最多读取的字符数。 如果在将指定数量的字符读入缓冲区之前就已达读取器的末尾,则返回该方法。

返回

Task<Int32>

表示异步读取操作的任务。 TResult 参数的值包含读入缓冲区的总字节数。 如果当前可用字节数少于所请求的字节数,则该结果值可小于所请求的字节数;如果已到达流结尾时,则为 0(零)。

示例

以下示例演示如何异步读取字符串的前 23 个字符。

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadCharacters();
        }

        static async void ReadCharacters()
        {
            string stringToRead = "Some characters to read but not all";
            char[] charsRead = new char[stringToRead.Length];

            using (StringReader reader = new StringReader(stringToRead))
            {
                await reader.ReadAsync(charsRead, 0, 23);
                Console.WriteLine(charsRead);
            }
        }
    }
}
// The example displays the following output:
// Some characters to read
//
ReadLine() 从当前字符串中读取一行字符并将数据作为字符串返回
public override string? ReadLine ();

返回

String

读取器中的下一行,或 null(如果已读取所有字符)。

示例

此代码示例是为 StringReader 类提供的一个更大示例的一部分。

// From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
string aLine, aParagraph = null;
StringReader strReader = new StringReader(textReaderText);
while(true)
{
    aLine = strReader.ReadLine();
    if(aLine != null)
    {
        aParagraph = aParagraph + aLine + " ";
    }
    else
    {
        aParagraph = aParagraph + "\n";
        break;
    }
}
Console.WriteLine("Modified text:\n\n{0}", aParagraph);
ReadLineAsync() 从当前字符串中异步读取一行字符并将数据作为字符串返回
public override System.Threading.Tasks.Task<string?> ReadLineAsync ();

返回

Task<String>

表示异步读取操作的任务。 TResult 参数的值包含来自字符串读取器的下一行或为 null 如果读取所有字符。

示例

以下示例演示如何从字符串异步读取一行。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadCharacters();
        }

        static async void ReadCharacters()
        {
            StringBuilder stringToRead = new StringBuilder();
            stringToRead.AppendLine("Characters in 1st line to read");
            stringToRead.AppendLine("and 2nd line");
            stringToRead.AppendLine("and the end");

            string readText;

            using (StringReader reader = new StringReader(stringToRead.ToString()))
            {
                while ((readText = await reader.ReadLineAsync()) != null)
                {
                    Console.WriteLine(readText);
                }
            }
        }
    }
}
// The example displays the following output:
//
// Characters in 1st line to read
// and 2nd line
// and the end
//
ReadToEnd() 读取从当前位置到字符串的结尾的所有字符并将它们作为单个字符串返回
public override string ReadToEnd ();

返回

String

从当前位置到基础字符串的结尾之间的内容。

示例

此代码示例是为 TextReader 类提供的一个更大示例的一部分。

static void ReadText(TextReader textReader)
{
    Console.WriteLine("From {0} - {1}",
        textReader.GetType().Name, textReader.ReadToEnd());
}
ReadToEndAsync() 异步读取从当前位置到字符串的结尾的所有字符并将它们作为单个字符串返回
public override System.Threading.Tasks.Task<string> ReadToEndAsync ();

返回

Task<String>

表示异步读取操作的任务。 TResult 参数值包括字符串来自当前位置到结束字符串字符。

示例

以下示例演示如何异步读取整个字符串。

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadCharacters();
        }

        static async void ReadCharacters()
        {
            StringBuilder stringToRead = new StringBuilder();
            stringToRead.AppendLine("Characters in 1st line to read");
            stringToRead.AppendLine("and 2nd line");
            stringToRead.AppendLine("and the end");

            using (StringReader reader = new StringReader(stringToRead.ToString()))
            {
                string readText = await reader.ReadToEndAsync();
                Console.WriteLine(readText);
            }
        }
    }
}
// The example displays the following output:
//
// Characters in 1st line to read
// and 2nd line
// and the end
//

♎ 更多方法

更多方法请查阅官方文档StringReader类。


⭐写在结尾:

文章中出现的任何错误请大家批评指出,一定及时修改。

希望写在这里的小伙伴能给个三连支持

相关文章:

  • Dapr 的 gRPC组件(又叫可插拔组件)的提案
  • Centos 8 安装 nginx
  • 【WSN】基于LGNDO算法实现传感器物理路由优化附matlab代码
  • 数据结构初步(六)- 复杂链表的分析与C语言实现
  • C语言-文件操作(最全)(二十一)
  • 时空数据挖掘五(城市计算)
  • mysql进阶:企业数据库安全防护方案
  • 会员营销体系构建需要满足的四个条件
  • C++----unordered_map unordered_set使用及模拟
  • R统计绘图-变量分组相关性网络图(igraph)
  • JUC并发编程系列详解篇五(线程基础理论进阶)
  • 设计模式 人类父母和猫孩子的关系理解观察者模式(发布订阅模式)
  • 【java核心技术】Java知识总结 -- 语法篇
  • Neo4j图数据库和GDS图算法应用
  • Hello, World
  • 2017-08-04 前端日报
  • Android优雅地处理按钮重复点击
  • leetcode-27. Remove Element
  • QQ浏览器x5内核的兼容性问题
  • uni-app项目数字滚动
  • 老板让我十分钟上手nx-admin
  • 如何合理的规划jvm性能调优
  • 手机端车牌号码键盘的vue组件
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • Android开发者必备:推荐一款助力开发的开源APP
  • elasticsearch-head插件安装
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • 阿里云ACE认证之理解CDN技术
  • ​草莓熊python turtle绘图代码(玫瑰花版)附源代码
  • ​如何在iOS手机上查看应用日志
  • (1)(1.11) SiK Radio v2(一)
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (全注解开发)学习Spring-MVC的第三天
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • .bat批处理(六):替换字符串中匹配的子串
  • .Net 路由处理厉害了
  • .net 提取注释生成API文档 帮助文档
  • .net6Api后台+uniapp导出Excel
  • .Net各种迷惑命名解释
  • .NET面试题解析(11)-SQL语言基础及数据库基本原理
  • .NET与java的MVC模式(2):struts2核心工作流程与原理
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...
  • @requestBody写与不写的情况
  • [ Linux 长征路第五篇 ] make/Makefile Linux项目自动化创建工具
  • [.net]官方水晶报表的使用以演示下载
  • [android] 练习PopupWindow实现对话框
  • [BJDCTF 2020]easy_md5
  • [BUUCTF 2018]Online Tool(特详解)