平常我們使用檔案儲存體程式資料時,一般需要將資料轉換為文本等儲存,等到需要使用的時候再從檔案讀取,然後重新構建類或結構或數組等對象。這樣的轉換和重建即浪費了時間效率不夠高又導致容易出錯,其實最近由於研究二進位檔案的存取,我找到了序列化存取的方法。
序列化存取可以做到類對象、結構、數組等記憶體實體整體儲存為二進位的檔案,而不需轉換,讀取的時候,通過還原序列化直接從檔案構建對象,而不需要重新通過轉換而來的資料構建,這樣既節省了轉換時間,運行速度又比較快。
簡要說明一下,序列化主要是通過實現System.Runtime.Serialization.IFormatter介面的System.Runtime.Serialization.Formatters.Binary.BinaryFormatter類來實現的,其中的void Serialize(Streamst, object ob)來實現序列化,這個函數就可以將object對象ob序列化為二進位流輸入給Stream流st,這個方法有重載,具體看MSDN,但對於我們只想簡單序列化,這樣寫也就夠了。object
Deserialize(Stream st)這個方法可以實現還原序列化,從給定的Stream流st中還原序列化出一個object對象,只需強制轉換為目標對象就可以了。
為了說明問題,建立了一個樣本工程(winForm程式),就一個介面,實現一個類的存和取如下(如果要序列化儲存結構和數組只需要將Person對象換成相應對象即可,另外設計器自動產生的程式碼沒有貼出來):
(程式調試)
(儲存的二進位檔案在VS中開啟的)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace 序列化的二進位檔案存取{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 隱藏檔位置 /// </summary> public static string savePath = "序列化隱藏檔.data";//指定儲存在程式開機檔案夾下的"序列化隱藏檔.data"內 /// <summary> /// 存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_Save_Click(object sender, EventArgs e) { //建立Person類對象 Person newPerson = new Person(); newPerson.name = textBox_name.Text; newPerson.age = Convert.ToInt32(textBox_age.Text); newPerson.grade = Convert.ToInt32(textBox_grade.Text); //這個對象用於序列化對象 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); ////或者用這個介面建立執行個體也可以 //System.Runtime.Serialization.IFormatter binaryFormatter // = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); ////序列化person對象newPerson,先轉化為二進位再存為檔案 //System.IO.MemoryStream ms = new System.IO.MemoryStream(); //binaryFormatter.Serialize(ms, newPerson); //byte[] buffer = ms.GetBuffer(); //System.IO.Stream st = new System.IO.FileStream(savePath, System.IO.FileMode.Create); //st.Write(buffer, 0, buffer.Length); //st.Close(); //序列化person對象newPerson並存為檔案 System.IO.Stream st = new System.IO.FileStream(savePath, System.IO.FileMode.Create);//建立檔案流 binaryFormatter.Serialize(st, newPerson);//序列化類別對象,並輸入資料流 st.Close(); } /// <summary> /// 取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_Read_Click(object sender, EventArgs e) { //這個對象用於還原序列化對象 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); //瀏覽儲存的對象檔案 OpenFileDialog op=new OpenFileDialog(){Filter="PerSon類檔案(*.data)|*.data",InitialDirectory=Application.StartupPath}; op.ShowDialog(); if (System.IO.File.Exist(op.FileName))//如果瀏覽到檔案 { System.IO.Stream st = new System.IO.FileStream(op.FileName, System.IO.FileMode.Open);//指向資料檔案的二進位流 Person per = (Person)binaryFormatter.Deserialize(st);//還原序列化,得到的是一個object對象,需要強制轉換 textBox_Show.Text = per.ToString();//顯示對象資訊 st.Close(); } else textBox_Show.Text = "沒有相關檔案!"; } } /// <summary> /// 類樣本 /// </summary> [Serializable]//加上這個屬性後此類建立的對象才能被序列化 class Person { public string name { set; get; } public int age { set; get; } public int grade { set; get; } public override string ToString() { return "名字:" + name.ToString() + "\r\n年齡:" + age.ToString() + "\r\n年級:" + grade.ToString(); } }}