正在加载...

Reading a File

在与程序同一文件夹里准备一个叫 note.txt 的纯文本文件。里面写一行短内容:oak。继续在 Pine 和 Main 里完成练习。用 File.ReadAllLines 读取,打印第一行,再用 dotnet run 运行。屏幕应显示 oak。第二个演示改为打开 missing.txt,并以 FileNotFoundException 结束。把用 C# 写文件留到下一页——本课请在编辑器里手动创建 note.txt。起始 public 类名保持 Pine。

读取磁盘上已有的那一行

前几页在你捕获或抛出的异常处停下。这一页要读到程序结束后仍留在文件里的字母。打开 Pine 文件。起始类名仍是 Pine。删掉残留的 throw new ArgumentException("empty")。删掉会打印 blocked 再打印 after 的 try/catch 演示。删掉裸露的 slots[5] 停止点,以及任何等待键盘输入的 ReadLine。程序不能等待键盘。
文件顶部保留两行:
using System;
using System.IO;
在 Main 里把磁盘文件名上的所有行读入数组:
string[] lines = File.ReadAllLines("note.txt");
打印第一行:
Console.WriteLine(lines[0]);
运行前在项目旁创建 note.txt,并在第一行准确输入 oak。本页的 C# 程序不会创建这个文本文件。

Pine prints oak from note.txt

csharp
using System;
using System.IO;

public class Pine
{
    public static void Main()
    {
        string[] lines = File.ReadAllLines("note.txt");
        Console.WriteLine(lines[0]);
    }
}

保存后,在同时含有项目和 note.txt 的文件夹里执行 dotnet run。屏幕显示 oak。File.ReadAllLines 打开了磁盘文件名并填满数组。索引零是第一行。程序不应为键盘暂停。若终端等你输入,说明文件里还有 ReadLine——删掉它。若看到 empty 或 ArgumentException 堆栈,说明 throwing 的残留还在运行。若看到 blocked 后 after,是旧的 try-catch。若只看到来自 slots 的 2,是 exceptions 残留。若看到 True 后 4,nullable 残留还在。
走一遍成功路径:Pine.Main,对 note.txt 调用 File.ReadAllLines,WriteLine 从索引零输出 oak。若没有 note.txt,很快就会进入缺失文件的故事——先创建文本文件。把 note.txt 放在你运行 dotnet run 的工作目录里。
这里的 ReadAllLines 指磁盘文件中的行,不是键盘提示。字母 oak 活在文本文件里;C# 类只负责读取。保留 using System.IO; 和 using System;。ReadAllText 是另一种方法——本页坚持 ReadAllLines 和 lines[0]。

📊 File read pieces on this page

Piece What it does on this page
File.ReadAllLines opens a disk name and returns a string array of lines
lines[0] the first line from that file
FileNotFoundException the stop when the disk name is missing

缺失的文件名会抛出 FileNotFoundException

第二个程序形状相同,但指向机器上不存在的名字:missing.txt。不要创建该文件。运行应因 FileNotFoundException 停止。

Pine missing file throws

csharp
using System;
using System.IO;

public class Pine
{
    public static void Main()
    {
        string[] lines = File.ReadAllLines("missing.txt");
        Console.WriteLine(lines[0]);
    }
}

用 dotnet run 运行。这次不会打印 oak。终端报告 FileNotFoundException。你前面已经练过 catch;这里的重点是:缺失的文件名会让读取停止。异常类型名 FileNotFoundException 的拼写很重要。项目可能在运行前仍会 build——缺失的磁盘名是运行时停止,不是缺分号。
若误创建了 missing.txt,删除或改名。想要安静的 oak 屏幕时优先用成功的 note.txt 演示。只在要看 FileNotFoundException 时用缺失名演示。不要把 throw new ArgumentException("empty") 当作唯一路径——那属于 throwing 页。
不要在本页用 C# 去创建 note.txt。下一页是 C# Writing a File。保留两条 using。去掉旧 catch,避免本该看到 oak 时却看到 blocked 和 after。

与 throwing 页的 throw 对比

在 throwing 页,当 cups 为空时 dotnet run 会因 ArgumentException 停止。本页缺失文件的停止是 FileNotFoundException,成功路径会从磁盘打印一行。throwing 文件只当历史保留;学习本课请运行文件读取教学文件。

你可以用 File.ReadAllLines 读取 note.txt 看到 oak。也可以打开 missing.txt 看到 FileNotFoundException。轮流用 dotnet run 运行每个演示。在项目旁手动创建 note.txt。Pine 仍是你用 dotnet run 打开的那扇门。下一页是写文件。
保留成功读取以获得安静屏幕。保留缺失名演示以观察停止。本页不要写 C# 写入。当 note.txt 存在时 dotnet run 后出现 oak,读取文件就完成了本课任务,可以进入 C# Writing a File。

🧠 测试您的知识

准备开始

测试您的知识

通过这个互动测验挑战自己,看看你对这个主题的理解程度如何

❓
5
问题
🎯
70%
及格要求
♾️
∞
时间
🔄
∞
尝试次数

📝 说明

  • 仔细阅读每个问题
  • 为每个问题选择最佳答案
  • 您可以随时重新参加测验
  • 您的进度将显示在顶部