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

时间:2018-10-21 19:05:25

标签: c# readline

我在让控制台输出我的students.txt文件中的所有行时遇到麻烦,目前我仅获得一个学生详细信息(学生详细信息每行一个学生)。这是我目前编写的大多数代码。

这是我用来阅读每一行并将该行细分为列表的类。

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);
            }
        }
    }
}

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

随着方法名称的实现,它会从第一行开始读取一行,以读取其他行,您应该重复该行:

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()

这也是我一直使用的,简短,简单,有效

答案 1 :(得分:1)

这是来自Microsoft文档的StreamReader的示例:

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循环,以读取文件中的所有行