0

我没有使用 StreamWriter 的 Filestream 的经验,所以我不确定是否有明显的我遗漏或不理解的东西。

我有一个方法使用其当前类中的属性,并将它们写入文件中的行,但它似乎只是将空白值写入文件。

public void FileWrite()
{
    FileStream fs = new FileStream("test.txt", FileMode.Append, FileAccess.Write);

    StreamWriter sw = new StreamWriter(fs);

    sw.WriteLine("Title = " + title);
    sw.WriteLine("Name = " + fName);
    sw.WriteLine("Last name = " + lName);
    sw.WriteLine("Gender = " + gender);
    sw.WriteLine("Medicare no = " + medicareNo);
    sw.WriteLine("Height = " + height);
    sw.WriteLine("Weight = " + weight);
    sw.WriteLine("Age = " + age);
    sw.WriteLine("Daily Recommended calories = " + cal);
    sw.WriteLine("Ideal Weight = " + idealWeight);

    sw.Flush();
    sw.Close();
    fs.Close();

    Readfile();
}

任何帮助将不胜感激,因为我过去没有使用过 Filestream 或 StreamWriter。

编辑:

public class PatientDetails
{
    public void ValuePass()
    {
        FileHandler file = new FileHandler();

        file.setTitle(this.title);
        file.setName(this.fName, this.lName);
        file.setMedicare(this.medicareNo);
        file.setGender(this.gender);
        file.setMeasurements(this.weight, this.height, this.age);
        file.setCalcs(this.cal, this.idealWeight);

        Console.WriteLine(this.fName + this.lName);
    }
}

这就是从不同类中传递值的方式。

并且这些值是从用户输入中获得的:

do
{
    Console.WriteLine("\nPlease enter the Title");
    this.title = Console.ReadLine();
4

2 回答 2

0

您的FileWrite()函数代码很好且正确。您应该首先检查 PatientDetail 类的属性是否具有值。如果他们这样做,那么file.setTitle()必须按如下方式实施:

file.setTitle(string text)
{
title = text; //title is the attribute of `FileHanlder` class 
}

也请调用函数file.FileWrite()ValuePass()

于 2013-09-15T07:52:45.393 回答
0

你可以做如下

FileHandler fh = new FileHandler();
fh.setTitle("New Title"); // set all the values.....
fh.FileWrite();
于 2013-09-15T07:02:42.333 回答