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]
相关推荐
麦兜*4 小时前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
萧鼎5 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
好望角雾眠6 小时前
第一阶段C#基础-10:集合(Arraylist,list,Dictionary等)
笔记·学习·c#
艾伦~耶格尔6 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
yujkss6 小时前
Python脚本每天爬取微博热搜-终版
开发语言·python
yzx9910136 小时前
小程序开发APP
开发语言·人工智能·python·yolo
啊阿狸不会拉杆7 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
武当豆豆8 小时前
C++编程学习(第25天)
开发语言·c++·学习
-Xie-10 小时前
Maven(二)
java·开发语言·maven
mftang10 小时前
Python可视化工具-Bokeh:动态显示数据
开发语言·python