C# StreamReader
上次修改时间:2025 年 5 月 2 日
C# StreamReader 教程展示了如何使用 C# 中的 StreamReader 读取文本文件。 C# 教程 是关于 C# 语言的综合教程。
在 C# 中,输入和输出操作基于流。 Stream 是用于处理字节序列的抽象基类。 流充当在各种上下文中读取和写入数据的中介,例如文件、输入/输出设备、进程间通信管道或 TCP/IP 套接字。
C# StreamReader
StreamReader 类有助于以指定的编码从字节流读取文本数据。 与直接处理字节不同,StreamReader 通过自动处理字符解码来简化文本处理。 默认情况下,它使用 UTF-8 编码,除非明确设置为其他编码。
The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece.
在以下示例中,我们将使用 StreamReader 从此文件读取数据。
C# StreamReader ReadToEnd 方法
ReadToEnd 方法检索从当前位置到流末尾的所有字符。 它将剩余内容作为字符串返回 - 如果已到达流的末尾,则返回一个空字符串。
var fileName = "thermopylae.txt"; using var sr = new StreamReader(fileName); string content = sr.ReadToEnd(); Console.WriteLine(content);
该示例一次性将文件读取到字符串中。
var fileName = "thermopylae.txt";
我们定义文件名。
using var sr = new StreamReader(fileName);
创建一个新的 StreamReader。 当 sr 变量超出范围时,using 关键字会释放 IO 资源。
string content = sr.ReadToEnd(); Console.WriteLine(content);
我们使用 ReadToEnd 读取文件的内容,并将它们打印到控制台。
$ dotnet run The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece.
C# StreamReader ReadBlock
ReadBlock 方法从当前流读取指定的最大字符数,并将数据写入缓冲区,从指定的索引开始。 它返回已读取的字符数。
var fileName = "thermopylae.txt";
using var sr = new StreamReader(fileName);
char[] buf = new char[25];
int n = sr.ReadBlock(buf, 0, buf.Length);
Console.WriteLine($"{n} characters read");
Console.WriteLine(buf);
该示例从文件中读取前 25 个字符。
char[] buf = new char[25];
我们定义字符缓冲区。
int c = sr.ReadBlock(buf, 0, buf.Length);
我们使用 ReadBlock 方法从文件中读取字符。 我们从第一个字符开始,并将数组的长度指定为要读取的字符数。
Console.WriteLine($"{n} characters read");
我们打印读取的字符数。
Console.WriteLine(buf);
我们打印字符数组的内容。
$ dotnet run 25 characters read The Battle of Thermopylae
C# StreamReader ReadLine
ReadLine 方法从当前流读取一行字符,并将数据作为字符串返回。 如果到达输入流的末尾,则返回 null。
var fileName = "thermopylae.txt";
using var sr = new StreamReader(fileName);
string? line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
该示例使用 ReadLine 方法逐行读取整个文件。
string? line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
我们使用 while 循环读取所有行。
C# StreamReader 读取网页
在以下示例中,我们从网页读取 HTML 数据。
using var httpClient = new HttpClient(); var url = "http://webcode.me"; var stream = await httpClient.GetStreamAsync(url); using var sr = new StreamReader(stream); string content = sr.ReadToEnd(); Console.WriteLine(content);
该示例读取网站的主页;它使用 HttpClient。 HttpClient 发送 HTTP 请求并从 URL 标识的资源接收 HTTP 响应。
using var httpClient = new HttpClient(); var url = "http://webcode.me";
创建 HttpClient 和 URL。
using var sr = new StreamReader(stream);
StreamReader 也可以将流作为参数。
var stream = await httpClient.GetStreamAsync(url);
我们异步地从 URL 读取数据。
string content = sr.ReadToEnd(); Console.WriteLine(content);
我们读取整个页面并将 HTML 数据打印到控制台。
$ dotnet run
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My html page</title>
</head>
<body>
<p>
Today is a beautiful day. We go swimming and fishing.
</p>
<p>
Hello there. How are you?
</p>
</body>
</html>
来源
在本文中,我们已经使用 C# 中的 StreamReader 读取了文本文件。
作者
列出所有 C# 教程。