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

csharp 复制代码
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();
        }
    }
}
使用文本保存对象状态存在的问题
  • 当对象属性发生变化时, 需要增加或减少信息的写入或读取次数
  • 信息安全性较差
  • 存对象的各种属性的顺序与读对象的各种属性的顺序要一致

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

csharp 复制代码
		//序列化保存对象
		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();
        }
  • 参与序列化与反序列化的类前加[Serializable]
相关推荐
淼淼76320 小时前
QT表格与数据
开发语言·qt
smile_Iris20 小时前
Day 38 GPU训练及类的call方法
开发语言·python
认真敲代码的小火龙21 小时前
【JAVA项目】基于JAVA的养老院管理系统
java·开发语言·课程设计
AI科技星21 小时前
统一场论质量定义方程:数学验证与应用分析
开发语言·数据结构·经验分享·线性代数·算法
扶苏-su21 小时前
Java---事件处理机制
java·开发语言
小灰灰搞电子21 小时前
Qt 实现炫酷锁屏源码分享
开发语言·qt·命令模式
电饭叔21 小时前
TypeError:unsupported operand type(s) for -: ‘method‘ and ‘int‘
开发语言·笔记·python
zfj32121 小时前
排查java应用内存溢出的工具和方法
java·开发语言·jvm·内存溢出
yugi98783821 小时前
MATLAB在卫星姿态控制系统中的应用
开发语言·matlab
历程里程碑21 小时前
C++ 7vector:动态数组的终极指南
java·c语言·开发语言·数据结构·c++·算法