c#对象保存Demo

2024-04-23 18:25:47 浏览数 (2)

一个将对象保存到文本文档的Demo供大家参考

这里写图片描述这里写图片描述
代码语言:javascript复制
namespace saveObjectWinForm
{   
    [Serializable]
    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; }
        public DateTime Birthday { get; set; }
    }
}
代码语言:javascript复制
namespace saveObjectWinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //保存到文本文件
        private void button1_Click(object sender, EventArgs e)
        {
            Student student = new Student()
            {
                Name = this.name.Text,
                Age = Convert.ToInt32(this.age.Text),
                Sex = this.sex.Text,
                Birthday = Convert.ToDateTime(this.brithday.Text),
            };
            FileStream fs = new FileStream("newObject.txt",FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(student.Name);
            sw.WriteLine(student.Age);
            sw.WriteLine(student.Sex);
            sw.WriteLine(student.Birthday);
            sw.Close();
            fs.Close();
        }
        //读取文本文件
        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("newObject.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            Student student = new Student()
            {
                Name = sr.ReadLine(),
                Age = Convert.ToInt32(sr.ReadLine()),
                Sex = sr.ReadLine(),
                Birthday = Convert.ToDateTime(sr.ReadLine()),

            };
            sr.Close();
            fs.Close();
            this.age.Text = (student.Age).ToString();
            this.name.Text = student.Name;
            this.brithday.Text = student.Birthday.ToString();
            this.sex.Text = student.Sex;
        }
        //保存二进制序列化形式
        private void button3_Click(object sender, EventArgs e)
        {
            //二进制序列化
            Student student = new Student()
            {
                Name = this.name.Text,
                Age = Convert.ToInt32(this.age.Text),
                Sex = this.sex.Text,
                Birthday = Convert.ToDateTime(this.brithday.Text),
            };
            //创建文件流
            FileStream fs = new FileStream("newObject.stu", FileMode.Create);
            //创建二进制格式化器
            BinaryFormatter formatter = new BinaryFormatter();
            //调用序列化方法
            formatter.Serialize(fs, student);
            fs.Close();

        }
        //读取二进制序列化形式
        private void button4_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("newObject.stu", FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();
            Student student = (Student)formatter.Deserialize(fs);
            fs.Close();
            this.age.Text = (student.Age).ToString();
            this.name.Text = student.Name;
            this.brithday.Text = student.Birthday.ToString();
            this.sex.Text = student.Sex;
        }
    }
}

0 人点赞