C# 读取文本文件

来源:这里教程网 时间:2026-02-17 08:44:22 作者:

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

C# 流

Stream为输入和输出的类型提供通用接口,并将编程器与操作系统和底层设备的特定详细信息隔离开。 例如,MemoryStream处理内存中的数据,FileStream处理文件中的数据。

thermopylae.txt

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.ReadAllText读取文本文件

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

Program.cs

using System;using System.IO;using System.Text;namespace ReadAllText{    class Program    {        static void Main(string[] args)        {            var path = @"C:\Users\Jano\Documents\thermopylae.txt";            string content = File.ReadAllText(path, Encoding.UTF8);            Console.WriteLine(content);        }    }}

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

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

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

Program.cs

using System;using System.IO;using System.Text;namespace ReadAllLines{    class Program    {        static void Main(string[] args)        {            var path = @"C:\Users\Jano\Documents\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# 使用StreamReader读取文本文件

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

使用StreamReader的ReadToEnd

ReadToEnd()方法从流的当前位置到其末尾读取所有字符。

Program.cs

using System;using System.IO;using System.Text;namespace StreamReaderReadToEnd{    class Program    {        static void Main(string[] args)        {            var path = @"C:\Users\Jano\Documents\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's 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's ReadToEnd()方法读取从当前位置到文件结尾的所有字符。

使用StreamReader的ReadLine

StreamReader的ReadLine()方法从当前流中读取一行字符,并将数据作为字符串返回。

Program.cs

using System;using System.IO;using System.Text;namespace StreamReaderReadLine{    class Program    {        static void Main(string[] args)        {            var path = @"C:\Users\Jano\Documents\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's ReadLine()方法逐行读取文件的内容。

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

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

Program.cs

using System;using System.IO;using System.Text;using System.Threading.Tasks;namespace ReadTextFileAsync{    class Program    {        static async Task Main(string[] args)        {            var path = @"C:\Users\Jano\Documents\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);        }    }}

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

static async Task Main(string[] args)

async修饰符允许在Main()方法中进行异步操作。

string content = await sr.ReadToEndAsync();

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

相关推荐