0

我正在从需要放入对象数组(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

我需要从此文件中读取数据并将其解析到我创建的员工数组中。这是 Employee 类的类数据:

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

2 回答 2

3

首先,我建议使用属性重新设计您的 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;
            }
        }

我没有在我的示例代码中包含任何错误检查,所以这取决于你。

于 2012-04-16T02:44:27.213 回答
0

我建议逐行阅读

请参阅 MSDN 上的示例

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

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

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("  ", " ");
}
于 2012-04-16T02:03:32.587 回答