这是流写入的方法代码

private void btnSave_Click(object sender, EventArgs e)
{
string file = @"d:\student.dat";
Stream stream = new FileStream(file, FileMode.Append, FileAccess.Write);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, list);
stream.Close();
}

怎么就 用FileMode.Append无法添加内容呢,但换成 FileMode.OpenOrCreate就可以,用还有个问题出现就是 用FileMode.OpenOrCreate,每次重新输入内容,上次写的内容,就会消失(好像是被覆盖刷新了一样)

完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace _8_4
{
public partial class frmDemo : Form
{ 
private StudnentList list=new StudnentList();
private int i = 0;
public frmDemo()
{
InitializeComponent();
}
#region 保存按钮
private void btnAdd_Click(object sender, EventArgs e)
{
Student student = new Student(textBox1.Text, textBox2.Text, textBox3.Text);
list[i] = student;
i++;
} 
#endregion
#region 添加按钮
private void btnSave_Click(object sender, EventArgs e)
{
string file = @"d:\student.dat";
Stream stream = new FileStream(file, FileMode.Append, FileAccess.Write);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, list);
stream.Close();
} 
#endregion
#region 显示按钮
private void btnShow_Click(object sender, EventArgs e)
{
string file = @"d:\student.dat";
Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
StudnentList students = (StudnentList)bf.Deserialize(stream);
int k = 0;
while (students[k] != null)
{
string s_no = students[k].student_no;
string name = students[k].name;
string depart = students[k].department;
int a = students[k].a;
string result = string.Format("{0}\t{1}\t{2}", s_no, name, depart );
lbResult.Items.Add(result);
k++;
}

stream.Close();

} 
#endregion

}
[Serializable]
public class Student
{
public int a = 3;
public string student_no;
public string name;
public string department;
public Student(string s_no, string name, string depart)
{
this.student_no = s_no;
this.name = name;
this.department = depart;
}
}
//studentlist类的
[Serializable]
public class StudnentList
{
private Student[] list = new Student[100];
public Student this[int index]
{
get
{
if (index < 0 || index >= 100)
{
return list[0];
}
else
{
return list[index];
}
}
set
{
if (!(index < 0 || index >= 100))
{
list[index] = value;
}
}
}
}

推荐图书

  • C++ Primer中文版(第4版)
  • More Effective C++:35个改善编程与设计的有效方法(中文版)
  • 设计模式:可复用面向对象软件的基础
  • C#与.NET 4高级程序设计(第5版)


1个回答

感觉你中间有几个问题:
1—— 你是要对数据进行追加,追加的数据是一个LIST,通过序列化为二进制流后追加。 这样的问题。。。很明显,假如追加成功的话,你这个文件里的数据将是多个LIST数据,你还能通过对这个文件进行反序列化后读取所有的数据吗?
2—— 你用的这个方案我从来没用过,所以也无法指出错误所在。 我想,你之所以APPEND会失败,那可能是因为你用了数据序列化的原因。 或者你可以这样:

private void btnSave_Click(object sender, EventArgs e)
{
    string file = @"d:\student.dat";
    Stream stream = new FileStream(file, FileMode.Append, FileAccess.Write);
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, list);
    ms.Seek(0, SeekOrigin.Begin);
    stream.Write(ms.ToArray(), 0, ms.Length);
    stream.Close();
}