将数据从文件读入类对象数组

时间:2012-04-16 01:06:33

标签: c# arrays file-io split

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

我需要读取此文件中的数据并将其解析为我创建的Employees数组。以下是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;
}

2 个答案:

答案 0 :(得分: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,以确保正确关闭文件。程序的其余部分很简单,只需逐行读取文件直到文件末尾。将每一行解析为所需类型,并将值设置为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错误检查,因此由您来完成。

答案 1 :(得分: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("  ", " ");
}