当前位置:   article > 正文

C#基础语法_对象的保存与读取

C#基础语法_对象的保存与读取
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TextFile
{
    public partial class FrmFile : Form
    {
        public FrmFile()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //封装数据
            Student stu1 = new Student()
            {
                StudentName = this.txtName.Text.Trim(),
                StudentAge = Convert.ToInt32(this.txtAge.Text.Trim()),
                StudentGender = this.txtGender.Text.Trim(),
                StudentBirthday = Convert.ToDateTime(this.txtBirthday.Text.Trim())
            };
            //保存到文件
            Directory.CreateDirectory(@"StudentInfo");
            FileStream fs = new FileStream(@"StudentInfo\Student.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(stu1.StudentName);
            sw.WriteLine(stu1.StudentAge);
            sw.WriteLine(stu1.StudentGender);
            sw.WriteLine(stu1.StudentBirthday);
            sw.Close();
            fs.Close();
            MessageBox.Show(stu1.StudentName + "信息保存成功");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"StudentInfo\Student.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            //一行一行读取
            Student duStudent = new Student()
            {
                StudentName = sr.ReadLine(),
                StudentAge = Convert.ToInt32(sr.ReadLine()),
                StudentGender = sr.ReadLine(),
                StudentBirthday = Convert.ToDateTime(sr.ReadLine())
            };
            //显示数据
            this.txtName.Text = duStudent.StudentName;
            this.txtAge.Text = duStudent.StudentAge.ToString();
            this.txtGender.Text = duStudent.StudentGender;
            this.txtBirthday.Text = duStudent.StudentBirthday.ToString();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
使用文本保存对象状态存在的问题
  • 当对象属性发生变化时, 需要增加或减少信息的写入或读取次数
  • 信息安全性较差
  • 存对象的各种属性的顺序与读对象的各种属性的顺序要一致

使用序列化和反序列化保存与还原对象

在这里插入图片描述

		//序列化保存对象
		private void btnSerialize_Click(object sender, EventArgs e)
        {
            //封装数据
            Student stu1 = new Student()
            {
                StudentName = this.txtName.Text.Trim(),
                StudentAge = Convert.ToInt32(this.txtAge.Text.Trim()),
                StudentGender = this.txtGender.Text.Trim(),
                StudentBirthday = Convert.ToDateTime(this.txtBirthday.Text.Trim())
            };
            //[1]创建文件流
            FileStream fs = new FileStream("objStudent.stu", FileMode.Create);
            //[2]创建二进制格式化器
            BinaryFormatter formatter = new BinaryFormatter();
            //[3]调用序列化方法
            formatter.Serialize(fs, stu1); //写入到哪里,以及要写入的内容
            //[4]关闭文件流
            fs.Close();
        }
		//反序列化读取文件内容		
        private void btnDeserialize_Click(object sender, EventArgs e)
        {
            //1.创建文件流
            FileStream fs = new FileStream("objStudent.stu", FileMode.Open);
            //2.创建二进制格式化器
            BinaryFormatter formatter = new BinaryFormatter();
            //3.调用反序列化方法
            Student objStudent = (Student)formatter.Deserialize(fs);
            //4.关闭文件流
            fs.Close();

            this.txtName.Text = objStudent.StudentName;
            this.txtAge.Text = objStudent.StudentAge.ToString();
            this.txtGender.Text = objStudent.StudentGender;
            this.txtBirthday.Text = objStudent.StudentBirthday.ToString();
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 参与序列化与反序列化的类前加[Serializable]
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/218126
推荐阅读
相关标签
  

闽ICP备14008679号