對象持久化是指將記憶體中的對象保存到可永久保存的存儲設備中(如磁碟)的一種技術。 本文介紹的是除資料庫之外的幾種對象持久化方式。 具體如下: 涉及知識點: 如下圖所示【主要功能是將用戶輸入的信息保存成各種格式,並從各個文檔中進行讀取出來】: 保存和讀取文本文檔代碼如下: 1 /// <summary> ...
對象持久化是指將記憶體中的對象保存到可永久保存的存儲設備中(如磁碟)的一種技術。
本文介紹的是除資料庫之外的幾種對象持久化方式。
具體如下:
- 保存成文本:即將記憶體對象以位元組流的方式保存到文本中。
- 序列化成Xml:即將對象以Xml的格式存儲。
- 序列化成Json:即將對象序列化成Json對象,然後存儲。
- 序列化成二進位:即將對象序列化成二進位位元組流保存到文件中。
涉及知識點:
- 序列化與反序列化
- 文件流的讀寫
- ListView顯示覆選框,並橫向排列
如下圖所示【主要功能是將用戶輸入的信息保存成各種格式,並從各個文檔中進行讀取出來】:
保存和讀取文本文檔代碼如下:
1 /// <summary> 2 /// 保存成文本 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnSaveTxt_Click(object sender, EventArgs e) 7 { 8 Dictionary<string, string> dicInfos = GetDictionaryInfos(); 9 string filePath = "objPerson.txt"; 10 //採用using關鍵字,會自動釋放 11 using (FileStream fs = new FileStream(filePath, FileMode.Create)) 12 { 13 using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) 14 { 15 foreach (var keyPair in dicInfos) 16 { 17 sw.WriteLine(string.Format("{0}={1}", keyPair.Key, keyPair.Value)); 18 } 19 } 20 } 21 } 22 23 /// <summary> 24 /// 從文本中讀取 25 /// </summary> 26 /// <param name="sender"></param> 27 /// <param name="e"></param> 28 private void btnReadTxt_Click(object sender, EventArgs e) 29 { 30 string filePath = "objPerson.txt"; 31 Dictionary<string, string> dic = new Dictionary<string, string>(); 32 //採用using關鍵字,會自動釋放 33 using (FileStream fs = new FileStream(filePath, FileMode.Open)) 34 { 35 using (StreamReader sw = new StreamReader(fs, Encoding.Default)) 36 { 37 while (!sw.EndOfStream) { 38 string lineInfo = sw.ReadLine(); 39 dic.Add(lineInfo.Split('=')[0], lineInfo.Split('=')[1]); 40 } 41 } 42 } 43 this.txtName.Text = dic["Name"]; 44 this.dtBirthday.Text = dic["Birthday"]; 45 if (dic["Gender"] == this.rbBoy.Text) 46 { 47 this.rbBoy.Checked = true; 48 } 49 else { 50 this.rbGirl.Checked = true; 51 } 52 string[] loves = dic["Love"].Split('|'); 53 foreach (var love in loves) 54 { 55 foreach (var item in this.lsvLove.Items) 56 { 57 ListViewItem li = item as ListViewItem; 58 if (li.Text == love) 59 { 60 li.Checked = true; 61 } 62 } 63 } 64 }View Code
保存和讀取Xml文檔代碼如下:
1 /// <summary> 2 /// 保存成Xml 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnSaveXml_Click(object sender, EventArgs e) 7 { 8 Person p = GetPersonInfos(); 9 string filePath = "objPerson.xml"; 10 using (FileStream fs = new FileStream(filePath, FileMode.Create)) 11 { 12 XmlSerializer serializer = new XmlSerializer(typeof(Person)); 13 serializer.Serialize(fs, p); 14 } 15 } 16 17 /// <summary> 18 /// 從Xml中讀取 19 /// </summary> 20 /// <param name="sender"></param> 21 /// <param name="e"></param> 22 private void btnReadXml_Click(object sender, EventArgs e) 23 { 24 string filePath = "objPerson.xml"; 25 Person p; 26 using (FileStream fs = new FileStream(filePath, FileMode.Open)) 27 { 28 XmlSerializer serializer = new XmlSerializer(typeof(Person)); 29 object obj= serializer.Deserialize(fs); 30 p = obj as Person; 31 } 32 SetPersonInfos(p); 33 }View Code
保存和讀取Json文檔如下:
1 /// <summary> 2 /// 保存成Json 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnSaveJson_Click(object sender, EventArgs e) 7 { 8 Person p = GetPersonInfos(); 9 JavaScriptSerializer jserializer = new JavaScriptSerializer(); 10 string strJson = jserializer.Serialize(p); 11 string strRegex = @"\\/Date\((\d+)\)\\/"; 12 MatchEvaluator evaluator = new MatchEvaluator(ConvertJsonDateToDateString); 13 14 //對時間進行處理,需要引用System.Text.RegularExpressions;命名空間 15 Regex reg = new Regex(strRegex); 16 strJson = reg.Replace(strJson, evaluator); 17 18 string filePath = "objPerson.json"; 19 using (FileStream fs = new FileStream(filePath, FileMode.Create)) 20 { 21 using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) 22 { 23 sw.Write(strJson); 24 } 25 } 26 } 27 28 /// <summary> 29 /// 從Json中讀取 30 /// </summary> 31 /// <param name="sender"></param> 32 /// <param name="e"></param> 33 private void btnReadJson_Click(object sender, EventArgs e) 34 { 35 JavaScriptSerializer jserializer = new JavaScriptSerializer(); 36 string filePath = "objPerson.json"; 37 Person p; 38 using (FileStream fs = new FileStream(filePath, FileMode.Open)) 39 { 40 using (StreamReader sw = new StreamReader(fs, Encoding.Default)) 41 { 42 string strJson = sw.ReadToEnd(); 43 string strRegex = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"; 44 MatchEvaluator evaluator = new MatchEvaluator(ConvertDateStringToJsonDate); //對時間進行處理 45 Regex reg = new Regex(strRegex); 46 strJson = reg.Replace(strJson, evaluator); 47 p = jserializer.Deserialize<Person>(strJson); 48 } 49 } 50 SetPersonInfos(p); 51 }View Code
保存和讀取Bin文檔如下:
1 /// <summary> 2 /// 保存成二進位文件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnSaveBin_Click(object sender, EventArgs e) 7 { 8 Person p = GetPersonInfos(); 9 string filePath = "objPerson.bin"; 10 using (FileStream fs = new FileStream(filePath, FileMode.Create)) { 11 BinaryFormatter bf = new BinaryFormatter(); 12 bf.Serialize(fs, p); 13 } 14 } 15 16 /// <summary> 17 /// 讀取二進位文件 18 /// </summary> 19 /// <param name="sender"></param> 20 /// <param name="e"></param> 21 private void btnReadBin_Click(object sender, EventArgs e) 22 { 23 string filePath = "objPerson.bin"; 24 Person p; 25 using (FileStream fs = new FileStream(filePath, FileMode.Open)) 26 { 27 BinaryFormatter bf = new BinaryFormatter(); 28 p= bf.Deserialize(fs) as Person; 29 } 30 SetPersonInfos(p); 31 }View Code
備註:其實對象持久化和對象序列化是兩個不同的概念。兩者有關聯卻不同。
對象持久化:是使對象可以保存到實體存儲介質中,在對象生命周期結束後可以再現。
對象序列化:是將對象或者數據結構轉化成特定的格式,使其可在網路中傳輸,或者可存儲在記憶體或者文件中。