在本章中,我们将探讨如何使用C#进行文件的读取和写入操作。这些操作对于处理各种数据文件、日志记录或用户数据非常有用。
文件流基础
文件流是用于读取和写入文件的数据通道。C# 提供了多种类来处理文件流,其中最常用的是 FileStream 类。FileStream 允许你以二进制或文本形式读取和写入文件。
创建文件流
首先,让我们看一个简单的例子,演示如何创建一个文件流并写入一些文本:
// www.javascriptcn.com code example
using System;
using System.IO;
class Program
{
static void Main()
{
// 定义文件路径
string filePath = "example.txt";
// 使用 FileStream 创建文件
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
// 写入文本
byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
fs.Write(info, 0, info.Length);
}
Console.WriteLine("File created and written successfully.");
}
}在这个例子中,我们使用 FileStream 的构造函数来创建一个新文件,并通过 Write 方法将文本写入文件。UTF8Encoding 用于将字符串转换为字节,以便写入文件。
读取文件
接下来,我们看看如何从文件中读取数据:
// www.javascriptcn.com code example
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
// 使用 FileStream 打开文件
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
// 创建一个字节数组来存储读取的数据
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
// 将字节转换回字符串
string text = System.Text.Encoding.UTF8.GetString(data);
Console.WriteLine(text);
}
}
}这个例子展示了如何打开已存在的文件,读取其中的内容,并将其转换回字符串显示出来。
使用 StreamReader 和 StreamWriter
虽然 FileStream 提供了基本的文件读写功能,但有时使用 StreamReader 和 StreamWriter 类会更方便。这两个类提供了更简洁的方式来处理文本文件。
使用 StreamWriter 写入文件
// www.javascriptcn.com code example
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
// 使用 StreamWriter 创建文件并写入文本
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.WriteLine("Hello, World!");
sw.WriteLine("This is another line.");
}
Console.WriteLine("File written successfully.");
}
}这里,我们使用 StreamWriter 来写入文本到文件中。WriteLine 方法可以自动添加换行符,使得每行文本都独立。
使用 StreamReader 读取文件
// www.javascriptcn.com code example
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.txt";
// 使用 StreamReader 读取文件
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}在上面的例子中,我们使用 StreamReader 来逐行读取文件的内容,并将其打印到控制台。
异步文件操作
对于大型文件或需要长时间运行的操作,异步编程模式(Async Programming Pattern)可以提高程序的响应性和性能。
异步写入文件
// www.javascriptcn.com code example
using System;
using System.IO;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string filePath = "example.txt";
// 使用异步方式写入文件
await File.WriteAllTextAsync(filePath, "Hello, World!");
Console.WriteLine("File written asynchronously.");
}
}WriteAllTextAsync 是一个便捷的方法,用于异步地将整个文本写入文件。
异步读取文件
// www.javascriptcn.com code example
using System;
using System.IO;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string filePath = "example.txt";
// 使用异步方式读取文件
string content = await File.ReadAllTextAsync(filePath);
Console.WriteLine(content);
}
}同样地,ReadAllTextAsync 可以异步地读取整个文件的内容。
总结
在这一章中,我们学习了如何使用C#进行文件的读取和写入操作。我们从基本的 FileStream 类开始,然后介绍了更高级的 StreamReader 和 StreamWriter 类,以及如何使用它们来进行异步文件操作。这些技术在处理文件时非常实用,可以帮助你高效地管理数据。