简单的InputOutput方法

abi*_*ash 0 c# console-application

我正在编写应该从文本文件中读取数据的简单方法.我无法理解为什么这个方法只读取第2行,第4行,第6行?方法如下.我的代码有什么问题?

public static List<Employee> ReadFromFile(string path = "1.txt") 
    {
        List<Employee> employees = new List<Employee>();
        Stream stream = null;
        StreamReader sr = null;
        try
        {
            stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            stream.Seek(0, SeekOrigin.Begin);
            sr = new StreamReader(stream);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                Employee employee = new DynamicEmployee();
                string str = sr.ReadLine();
                employee.FirstName = str.Substring(1, 20).Trim();
                employee.LasttName = str.Substring(20, 20).Trim();
                employee.Paynment = Convert.ToDouble(str.Substring(40, 20).Trim());
                Console.WriteLine("{0} {1} {2}", employee.FirstName, employee.LasttName, employee.Paynment);
                employees.Add(employee);
                //Console.WriteLine(str);
            }
        }
        catch//(System.FormatException)
        {
            Console.WriteLine("File format is incorect");
        }
        finally
        {
            sr.Close();
            stream.Close();
        }
        return employees;
    }
Run Code Online (Sandbox Code Playgroud)

Luk*_*ton 5

你打了line = sr.ReadLine()两次电话.

删除此行,string str = sr.ReadLine();并使用变量line