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]
相关推荐
侃侃_天下29 分钟前
最终的信号类
开发语言·c++·算法
echoarts1 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix1 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz1 小时前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题2 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 小时前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号3 小时前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_3 小时前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty3 小时前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序