将数据从文件读取到类对象数组中

本文关键字:对象 数组 读取 数据 文件 | 更新日期: 2023-09-27 18:31:08

我正在从需要放入对象数组(myEmployees)的文件中读取数据。我相信我的代码在此示例结束之前是正确的,但我不确定如何从文件中读取数据,拆分数据,然后将其正确放入我的类对象数组中。

//declare an array of employees
Employee[] myEmployees = new Employee[10];
//declare other variables
string inputLine;
string EmpName;
int EmpNum;
double EmpWage;
double EmpHours;
string EmpAdd;
//declare filepath
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "''";
//get input
Console.Write("'nEnter a file name in My Documents: ");
string input = Console.ReadLine();
string path = environment + input;
Console.WriteLine("Opening the file...");
//read file
StreamReader myFile = new StreamReader(path);
inputLine = (myFile.ReadLine());

所以我正在从结构如下的文件中读取数据:

Employee Number
Employee Name
Employee Address
Employee wage Employee Hours

我需要从此文件中读取数据并将其解析到我创建的员工数组中。以下是班级员工的班级数据:

public void Employeeconst ()
{
    employeeNum = 0;
    name = "";
    address = "";
    wage = 0.0;
    hours = 0.0;
}
public void SetEmployeeNum(int a)
{
    employeeNum = a;
}
public void SetName(string a)
{
    name = a;
}
public void SetAddress(string a)
{
    address = a;
}
public void SetWage(double a)
{
    wage = a;
}
public void SetHours(double a)
{
    hours = a;
}
public int GetEmployeeNum()
{
    return employeeNum;
}
public string GetName()
{
    return name;
}
public string GetAddress()
{
    return address;
}
public double GetWage()
{
    return wage;
}
public double GetHours()
{
    return hours;
}

将数据从文件读取到类对象数组中

首先,我建议使用属性重新设计您的 Employee 类,它更具可读性,并且更好地遵循面向对象编程的原则:

public class Employee
{
    public int EmployeeNum { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public double Wage { get; set; }
    public double Hours { get; set; }
    public void Employee()
    {
        EmployeeNum = 0;
        Name = "";
        Address = "";
        Wage = 0.0;
        Hours = 0.0;
    }
}

还可以考虑将 StreamReader 包装在"using"关键字中,以确保正确关闭文件。程序的其余部分很简单,只需逐行读取文件直到文件结束。将每一行解析为所需的类型,并将值设置为 Employee 对象:

        using(StreamReader myFile = new StreamReader(path))
        {
            int index = 0;
            while(!myFile.EndOfStream)
            {
                Employee E = new Employee();
                E.EmployeeNum = Int32.Parse(myFile.ReadLine());
                E.Name = myFile.ReadLine();
                E.Address = myFile.ReadLine();
                E.Wage = Double.Parse(myFile.ReadLine());
                E.Hours = Double.Parse(myFile.ReadLine());
                myEmployees[index++] = E;
            }
        }

我没有在我的示例代码中包含 eny 错误检查,所以由您来做。

我建议逐行阅读

请参阅MSDN上的示例

http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

对于您阅读的每一行,您将有一个字符串 - 您可以使用字符串将此字符串拆分为数组。分裂。

string mystring = "50305 FirstName LastName 1234 Anywhere Place 133.25 40";
string[] myarray = mystring.Split(' ');

但是,我建议处理双空格等的字符串输入。

你可以做这样的事情来摆脱重复的空格。

string mynewstring = mystring.Replace("  ", " ");
while (mynewstring.Contains("  "))
{
  mynewstring = mystring.Replace("  ", " ");
}