C#对象序列化、反序列化、保存、读取、对象直接保存、读取

        基于WindowForm应用程序C#语言通过实际案例实现将对象保存到文件及从已保存的文件中读取对象(直接保存与读取、通过序列化与反序列化方式进行对象保存与读取)

添加Student类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WF_Serialize
{
    /// <summary>
    /// 学生类、对象
    /// </summary>
    
    [Serializable]     //进行对象序列化与反系列话必须添加
    class Student
    {
        public string Name { get; set; }         //属性
        public string Gender { get; set; }
        public int Age { get; set; }
        public DateTime Birthday{ get; set; }
    }
}

主程序引入命名空间

// 引入必要的命名空间
using System.IO; //数据流命名空间
using System.Runtime.Serialization.Formatters.Binary;     //序列化与反序列化命名空间

对象序列化反序列化保存与读取和对象直接保存与读取实现方法程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
 
//数据流命名空间
using System.IO; 
//序列化与反序列化命名空间
using System.Runtime.Serialization.Formatters.Binary;  
 
namespace WF_Serialize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnSave_Click(object sender, EventArgs e)
        {
            //从控件中读取数据并封装成对象
            Student objStudent = new Student()
            {
                Name = this.txtName.Text.Trim(),
                Age = Convert.ToInt32(this.txtAge.Text.Trim()),
                Birthday = Convert.ToDateTime(this.txtDate.Text.Trim()),
                Gender = this.txtGender.Text.Trim()
            };
            //调用方法将对象保存到文本中
            SaveStudent(objStudent);
        }
 
        //对象到文本文件中的方法
        private void  SaveStudent(Student objStudent)
        {
            //保存对象到文本文件中
            FileStream fs = new FileStream("objStudent.obj", FileMode.Create);  
            //创建写入器
            StreamWriter sw = new StreamWriter(fs);
            //逐行写入数据
            sw.WriteLine(objStudent.Name);
            sw.WriteLine(objStudent.Age);
            sw.WriteLine(objStudent.Birthday);
            sw.WriteLine(objStudent.Gender);
            sw.Close();  //关闭写入器
            fs.Close();  //关闭数据流
        }
 
        private void btnRead_Click(object sender, EventArgs e)
        {
            //读取对象到窗体
            FileStream fs = new FileStream("objStudent.obj", FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            //逐行读取文本数据,并封装成对象
            Student objStudent = new Student()
            {
                Name = sr.ReadLine(),
                Age = Convert.ToInt32(sr.ReadLine()),
                Birthday = Convert.ToDateTime(sr.ReadLine()),
                Gender = sr.ReadLine()
            };
            sr.Close();  
            fs.Close();  //关闭数据流
            //显示对象
            this.txtName.Text = objStudent.Name;
            this.txtGender.Text = objStudent.Gender;
            this.txtDate.Text = objStudent.Birthday.ToShortDateString();
            this.txtAge.Text = objStudent.Age.ToString();
        }
 
        private void btnSerialize_Click(object sender, EventArgs e)
        {
            //从控件中读取数据并封装成对象
            Student objStudent = new Student()
            {
                Name = this.txtName.Text.Trim(),
                Age = Convert.ToInt32(this.txtAge.Text.Trim()),
                Birthday = Convert.ToDateTime(this.txtDate.Text.Trim()),
                Gender = this.txtGender.Text.Trim()
            };
            //保存对象到文本文件中(序列化)
            FileStream fs = new FileStream("objStudent1.obj", FileMode.Create);
            //创建二进制格式化器
            BinaryFormatter bf =new BinaryFormatter();
            //调用序列化方法
            bf.Serialize(fs, objStudent);
            //关闭数据流
            fs.Close();  
        }
 
        private void btnUnserialize_Click(object sender, EventArgs e)
        {
            //读取对象到窗体
            FileStream fs = new FileStream("objStudent1.obj", FileMode.Open);           
            BinaryFormatter bf = new BinaryFormatter();
            //通过反序列化还原对象
            Student objStudent=(Student)bf.Deserialize(fs);
            fs.Close();
            //显示对象
            this.txtName.Text = objStudent.Name;
            this.txtGender.Text = objStudent.Gender;
            this.txtDate.Text = objStudent.Birthday.ToShortDateString();
            this.txtAge.Text = objStudent.Age.ToString();
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值