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();
}
}
}
本示例的工程源码见:https://download.csdn.net/download/cqfdcw/10829376