reader.readLine() 只读取txt文档的第一行

reader.readLine() only reading first line of txt document

我无法让我的控制台输出我的 students.txt 文件中的所有行,目前我只能获得一个学生的详细信息(学生详细信息是每行一个学生)。这是我目前编写的大部分代码。

这是我用来读取每一行并将分解的行存储到列表中的 class。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Test
{
    class QA
    {
        public List<Student> students;

        public QA()
        {
            students = new List<Student>();
            string line;
            using (StreamReader reader = new StreamReader("/Users/jvb/Desktop/Test/Test/Students.txt"))
            {
                    line = reader.ReadLine();
                    var s = line.Split(',');
                    int id = int.Parse(s[0]);
                    int houseNo = int.Parse(s[3]);
                    var status = int.Parse(s[7]);
                    Student sData = new Student(id, s[1], s[2], houseNo, s[4], s[5], s[6], (StudentStatus)status);
                    AddStudent(sData);
            }
        }


        public List<Student> GetStudents()
        {
            return students;
        }

        public void AddStudent(Student student)
        {
            students.Add(student);
        }
    }
}

这是我用来向用户输出所有内容的 program.cs 文件,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            QA qa = new QA();

            foreach (var s in qa.GetStudents())
            {
                Console.WriteLine(s);
            }
        }
    }
}

非常感谢任何帮助!

正如方法名所实现的那样,它读取一行,从第一行开始,读取其他行你应该重复它:

using (StreamReader reader = new StreamReader("/Users/jvb/Desktop/Test/Test/Students.txt"))
{
     while(!reader.EndOfStream)
     {             
          line = reader.ReadLine();
          var s = line.Split(',');
          int id = int.Parse(s[0]);
          int houseNo = int.Parse(s[3]);
          var status = int.Parse(s[7]);
          Student sData = new Student(id, s[1], s[2], houseNo, s[4], s[5], s[6], (StudentStatus)status);
          AddStudent(sData);
     }
}

或者如@CamiloTerevinto 所说,要阅读所有行,您可以尝试

File.ReadAllLines()

这也是我一直使用的,它简短、简单、高效

下面是 StreamReader 的 Microsoft 文档中的示例:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e) 
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}

注意 using 块中的 while 循环。您的代码也应该有一个类似的 while 循环来读取文件中的所有行