序列化
for (int i = 0; i < phi.Count; i++)
{
FileStream fileStream = null;
//定义一个文件流
fileStream = new FileStream("enterInfo.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fileStream);
sw.WriteLine("入库单相信信息表");
sw.WriteLine("入库单编号" + phi[i].Coding);
sw.WriteLine("药品编号" + phi[i].Storeroomcoding);
sw.WriteLine("名称" + phi[i].Name);
sw.WriteLine("进价" + phi[i].JoinPrice);
sw.WriteLine("售价" + phi[i].Price);
sw.WriteLine("差价" + phi[i].PriceIfference);
sw.WriteLine("数量" + phi[i].Count);
sw.WriteLine("进价总额" + phi[i].JoinLend);
sw.WriteLine("销售总额" + phi[i].Distribution);
num=num+phi[i].Count;
//关闭文件流
sw.Close();
fileStream.Close();
}
phi是一个集合,我想把这个集合里的东西都打印出来,可是最后打印的却是这个集合中的最后一行,看这代码有没有可以改进的,帮我实现最后效果。如果不行请帮忙给出其他代码(注:将集合中全部对象的属性值打印出来)。
------解决方案--------------------
你每次都创建文件,然后写入内容,覆盖前面的内容,结果就是最后一条记录被 保存在文件里。
改成下面的样子吧
FileStream fileStream = null;
//定义一个文件流
fileStream = new FileStream("enterInfo.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fileStream);
for (int i = 0; i < phi.Count; i++)
{
sw.WriteLine("入库单相信信息表");
sw.WriteLine("入库单编号" + phi[i].Coding);
sw.WriteLine("药品编号" + phi[i].Storeroomcoding);
sw.WriteLine("名称" + phi[i].Name);
sw.WriteLine("进价" + phi[i].JoinPrice);
sw.WriteLine("售价" + phi[i].Price);
sw.WriteLine("差价" + phi[i].PriceIfference);
sw.WriteLine("数量" + phi[i].Count);
sw.WriteLine("进价总额" + phi[i].JoinLend);
sw.WriteLine("销售总额" + phi[i].Distribution);
num=num+phi[i].Count;
//关闭文件流
}
sw.Close();
fileStream.Close();