我没有使用 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();