开放的编程资料库

当前位置:我爱分享网 > C#教程 > 正文

C#读取文本文件

在这篇文章中,我们展示了如何在C#中读取文本文件。

C#中的输入和输出基于流。Stream是所有流的抽象基类。流是字节序列的抽象,例如文件、输入/输出设备、进程间通信管道或TCP/IP套接字。

C#流

Stream为输入和输出类型提供通用接口,并将程序员与操作系统和底层设备的具体细节隔离开来。例如,MemoryStream处理内存中的数据,FileStream处理文件中的数据。

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#使用File.ReadLines读取文本文件

File.ReadLines返回文件所有行的IEnumerable。完成迭代后,它会关闭文件。与将整个数据作为字符串或字符串数​​组返回的方法相比,它占用的资源要少得多。

using System.Text;

var path = "thermopylae.txt";

var enumLines = File.ReadLines(path, Encoding.UTF8);

foreach (var line in enumLines)
{
    Console.WriteLine(line);
}

在示例中,我们在foreach循环中遍历行的枚举并逐行打印内容。

C#使用File.ReadAllLines读取文本文件

File.ReadAllLines打开一个文本文件,将文件的所有行读入字符串数组,然后关闭文件。

using System.Text;

var path = "thermopylae.txt";

string[] lines = File.ReadAllLines(path, Encoding.UTF8);

foreach (string line in lines)
{
    Console.WriteLine(line);
}

使用File.ReadAllLines方法读取thermopylae.txt文件的内容并将其打印到控制台。

foreach (string line in lines)
{
    Console.WriteLine(line);
}

我们遍历数组并打印它的元素。

C#使用File.ReadAllText读取文本文件

File.ReadAllText方法打开一个文本文件,将文件的所有行读入一个字符串,然后关闭文件。

using System.Text;

var path = "thermopylae.txt";

string content = File.ReadAllText(path, Encoding.UTF8);
Console.WriteLine(content);

该示例读取thermopylae.txt文件的内容并将它们打印到控制台。

C#使用StreamReader读取文本文件

StreamReader专为特定编码的字符输入而设计。它用于从标准文本文件中读取信息行。

使用StreamReader的ReadToEnd

ReadToEnd方法从流的当前位置读取所有字符,直到结束。

using System.Text;

var path = "thermopylae.txt";

using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs, Encoding.UTF8);

string content = sr.ReadToEnd();

Console.WriteLine(content);

该示例使用StreamReader的ReadToEnd方法读取文件。

using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);

FileStream类为文件提供了一个Stream,支持同步和异步读写操作。构造函数使用指定的路径、创建模式和读/写权限初始化FileStream类的新实例。

using var sr = new StreamReader(fs, Encoding.UTF8);

FileStream被传递给StreamReader

string content = sr.ReadToEnd();

StreamReader的ReadToEnd方法读取从当前位置到文件末尾的所有字符。

使用File.OpenRead

有一个File.OpenRead辅助方法来创建一个FileStream

using System.Text;

var path = "thermopylae.txt";

using var fs = File.OpenRead(path);
using var sr = new StreamReader(fs, Encoding.UTF8);

string content = sr.ReadToEnd();

Console.WriteLine(content);

该示例使用File.OpenRead创建一个FileStream,将流传递给StreamReader并使用ReadToEnd.

使用StreamReader的ReadLine

StreamReaderReadLine方法从当前流中读取一行字符并将数据作为字符串返回。

using System.Text;

var path = "thermopylae.txt";

using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs, Encoding.UTF8);

string line = String.Empty;

while ((line = sr.ReadLine()) != null)
{
    Console.WriteLine(line);
}

代码示例逐行读取文件。

string line = String.Empty;

while ((line = streamReader.ReadLine()) != null)
{
        Console.WriteLine(line);
}

在一个while循环中,我们使用StreamReader的ReadLine方法逐行读取文件的内容。

C#使用File.OpenText读取文本

File.OpenText方法打开现有的UTF-8编码文本文件以供阅读。它是一种快速使用StreamReader的辅助方法。

using System.Text;

var path = "thermopylae.txt";

using StreamReader sr = File.OpenText(path);
string content = sr.ReadToEnd();

Console.WriteLine(content);

该示例使用File.OpenText打开一个文本文件,并使用ReadToEnd读取其内容。

C#使用StreamReader的ReadToEndAsync异步读取文本文件

ReadToEndAsync方法异步读取从当前位置到流末尾的所有字符,并将它们作为一个字符串返回。

using System.Text;

var path = "thermopylae.txt";

using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs, Encoding.UTF8);

string content = await sr.ReadToEndAsync();
Console.WriteLine(content);

在下一个例子中,我们异步读取一个文本文件。

string content = await sr.ReadToEndAsync();

await运算符应用于异步方法中的任务,以暂停该方法的执行,直到等待的任务完成。

在这篇文章中,我们在C#中以各种方式读取文本文件。

列出所有C#教程。

未经允许不得转载:我爱分享网 » C#读取文本文件

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏