读取和写入文件
学习如何使用 C# 创建、读取和写入文件
读取和写入文件 是 CoddyKit 上的免费 C# Academy 课时。 这是第 1 节课,共 5 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 5 节课。
文件输入/输出简介
读取和写入文件
欢迎来到“文件和数据处理”类别的第一课!在本课中,您将学习如何在 C# 中读取和写入文件,从而能够在文本文件中存储和获取数据。让我们开始吧!

为什么要使用文件?
文件可以让程序持久化数据,使数据在应用程序关闭后仍然可用。无论是存储用户数据、日志还是配置,文件处理都是开发者必须掌握的重要技能。
要点:C# 提供了 File、StreamReader 和 StreamWriter 等内置类来处理文件操作。
读取文件
要读取文件内容,对于小型文件请使用 File.ReadAllText() 方法,对于较大文件请使用 StreamReader。示例:
要点:尝试读取文件前,务必检查文件是否存在。
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "example.txt";
// Reading a file using File.ReadAllText()
if (File.Exists(filePath)) {
string content = File.ReadAllText(filePath);
Console.WriteLine("File Content:");
Console.WriteLine(content);
} else {
Console.WriteLine("File does not exist.");
}
}
}
写入文件
要将数据写入文件,对于小型文件请使用 File.WriteAllText() 方法,对于需要更多控制的情况请使用 StreamWriter。示例:
提示:使用 File.AppendAllText() 可以向现有文件添加数据,而不会覆盖原有内容。
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "output.txt";
string content = "Hello, this is a test file.";
// Writing to a file using File.WriteAllText()
File.WriteAllText(filePath, content);
Console.WriteLine("File written successfully.");
}
}
逐行读取文件
对于大型文件,使用 StreamReader 逐行读取会更加高效。示例:
要点:使用 using 语句确保文件在读取完成后正确关闭。
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "example.txt";
// Reading a file line by line using StreamReader
if (File.Exists(filePath)) {
using (StreamReader reader = new StreamReader(filePath)) {
string line;
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
} else {
Console.WriteLine("File does not exist.");
}
}
}
向文件追加数据
要向现有文件添加数据,请使用 File.AppendAllText(),或使用 StreamWriter 并将 append 标志设置为 true。示例:
提示:使用 Environment.NewLine 确保正确换行。
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "output.txt";
string newContent = "This line will be appended.";
// Appending data to a file
File.AppendAllText(filePath, newContent + Environment.NewLine);
Console.WriteLine("Data appended successfully.");
}
}
练习挑战
创建一个程序,将用户输入的一系列名称写入文件,然后读取文件并显示这些名称。示例:
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "names.txt";
// Writing user-entered names to a file
Console.WriteLine("Enter names (type 'exit' to stop):");
using (StreamWriter writer = new StreamWriter(filePath)) {
string name;
while ((name = Console.ReadLine()) != "exit") {
writer.WriteLine(name);
}
}
// Reading the file
Console.WriteLine("\nNames in the file:");
using (StreamReader reader = new StreamReader(filePath)) {
string line;
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
}
}
文件处理的最佳实践
以下是一些最佳实践:
- 读取或追加数据前,务必检查文件是否存在。
- 使用
using语句自动关闭文件流。 - 处理异常,以避免在处理缺失或被锁定的文件时发生崩溃。
提示:正确的文件处理可以确保您的程序健壮且无错误。
文件输入/输出回顾
做得很好!
恭喜您!您已经学会了如何在 C# 中读取和写入文件。在下一课中,我们将探索 JSON 和 XML 数据格式的使用。让我们继续编写代码吧!

用 AI 导师学习 C# — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 93
- 课程
- 346
常见问题解答
「读取和写入文件」课时是免费的吗?
是的 — 「读取和写入文件」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 5 节课。
「读取和写入文件」这节课中我会学到什么?
学习如何使用 C# 创建、读取和写入文件 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C# Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 5 节。
「读取和写入文件」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C# Academy 课中编写并运行代码吗?
能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。