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]
相关推荐
北冥湖畔的燕雀2 小时前
C++泛型编程(函数模板以及类模板)
开发语言·c++
QX_hao3 小时前
【Go】--map和struct数据类型
开发语言·后端·golang
你好,我叫C小白3 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
Evand J5 小时前
【MATLAB例程】基于USBL和DVL的线性回归误差补偿,对USBL和DVL导航数据进行相互补偿,提高定位精度,附代码下载链接
开发语言·matlab·线性回归·水下定位·usbl·dvl
爱喝白开水a6 小时前
LangChain 基础系列之 Prompt 工程详解:从设计原理到实战模板_langchain prompt
开发语言·数据库·人工智能·python·langchain·prompt·知识图谱
Neverfadeaway6 小时前
【C语言】深入理解函数指针数组应用(4)
c语言·开发语言·算法·回调函数·转移表·c语言实现计算器
武子康6 小时前
Java-152 深入浅出 MongoDB 索引详解 从 MongoDB B-树 到 MySQL B+树 索引机制、数据结构与应用场景的全面对比分析
java·开发语言·数据库·sql·mongodb·性能优化·nosql
杰克尼6 小时前
JavaWeb_p165部门管理
java·开发语言·前端
一成码农6 小时前
JavaSE面向对象(下)
java·开发语言
偶尔的鼠标人7 小时前
Avalonia DataGrid 控件的LostFocus事件会多次触发
开发语言·c#