You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C# StreamReader.ReadLine()读取并存储答题数据文件内容?

用C#的StreamReader.ReadLine()读取并存储客观题答题数据

没问题,我来帮你搞定这个需求。首先咱们得明确文件的结构:第一行是20位的标准答案,之后每一行都是4位学号 + 20位答题内容,所以咱们的核心思路就是逐行读取,区分第一行和后续行,分别处理存储。

第一步:定义数据结构

为了清晰存储学生的答题信息,咱们可以先创建一个简单的类来封装学号和答题内容:

public class StudentAnswer
{
    public string StudentId { get; set; }
    public string Answers { get; set; }
}

第二步:编写读取和存储逻辑

接下来就是用StreamReader.ReadLine()来逐行处理文件了,这里要注意处理文件不存在、格式不符合预期的情况,避免程序崩溃:

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string filePath = "sample.txt"; // 替换成你的实际文件路径
        string standardAnswer = string.Empty;
        List<StudentAnswer> studentAnswers = new List<StudentAnswer>();

        try
        {
            // 使用using语句自动释放StreamReader资源,不用手动关流
            using (StreamReader reader = new StreamReader(filePath))
            {
                // 读取第一行:标准答案
                standardAnswer = reader.ReadLine();
                // 校验标准答案格式是否合规
                if (string.IsNullOrEmpty(standardAnswer) || standardAnswer.Length != 20)
                {
                    Console.WriteLine("文件第一行格式错误,应为20位字符的标准答案");
                    return;
                }

                string line;
                // 循环读取后续每行内容,直到文件末尾
                while ((line = reader.ReadLine()) != null)
                {
                    // 跳过空行,避免无效数据干扰
                    if (string.IsNullOrWhiteSpace(line))
                        continue;

                    // 校验每行长度:4位学号+20位答题内容=24位
                    if (line.Length != 24)
                    {
                        Console.WriteLine($"行内容格式错误:{line},应为4位学号+20位答题内容");
                        continue;
                    }

                    // 拆分学号和答题内容
                    string studentId = line.Substring(0, 4);
                    string answers = line.Substring(4, 20);

                    studentAnswers.Add(new StudentAnswer
                    {
                        StudentId = studentId,
                        Answers = answers
                    });
                }
            }

            // 这里可以添加后续逻辑,比如打印数据、写入数据库等
            Console.WriteLine($"成功读取标准答案:{standardAnswer}");
            Console.WriteLine($"共读取{studentAnswers.Count}名学生的答题数据:");
            foreach (var student in studentAnswers)
            {
                Console.WriteLine($"学号:{student.StudentId},答题:{student.Answers}");
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine($"未找到目标文件:{filePath}");
        }
        catch (IOException ex)
        {
            Console.WriteLine($"读取文件时发生IO错误:{ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生未知错误:{ex.Message}");
        }
    }
}

关键细节说明

  • using语句:这是C#处理文件流的最佳实践,能确保StreamReader使用完毕后自动释放系统资源,不用手动调用Close()或Dispose()。
  • 格式校验:每一步都加了长度校验,避免因为文件内容不符合规则导致后续逻辑出错,比如第一行必须20位,后续行必须24位。
  • 空行处理:跳过空行,避免无效数据干扰存储逻辑。
  • 异常捕获:覆盖了常见的文件操作异常,让程序更健壮,不会因为一点小问题就崩溃。

存储扩展

如果需要把数据持久化,比如存入数据库或者写入另一个文件,只需要在读取完成后,对standardAnswer和studentAnswers集合进行相应操作即可。比如写入CSV文件、用EF Core存入SQL Server,都是很容易扩展的。

内容的提问来源于stack exchange,提问作者David Shi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:22:35