C# 對象持久化詳解。本站提示廣大學習愛好者:(C# 對象持久化詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C# 對象持久化詳解正文
對象持久化是指將內存中的對象保存到可永久保存的存儲設備中(如磁盤)的一種技術。
本文介紹的是除數據庫之外的幾種對象持久化方式。
具體如下:
涉及知識點:
如下圖所示【主要功能是將用戶輸入的信息保存成各種格式,並從各個文檔中進行讀取出來】:
保存和讀取文本文檔代碼如下:
/// <summary> /// 保存成文本 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveTxt_Click(object sender, EventArgs e) { Dictionary<string, string> dicInfos = GetDictionaryInfos(); string filePath = "objPerson.txt"; //采用using關鍵字,會自動釋放 using (FileStream fs = new FileStream(filePath, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { foreach (var keyPair in dicInfos) { sw.WriteLine(string.Format("{0}={1}", keyPair.Key, keyPair.Value)); } } } } /// <summary> /// 從文本中讀取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnReadTxt_Click(object sender, EventArgs e) { string filePath = "objPerson.txt"; Dictionary<string, string> dic = new Dictionary<string, string>(); //采用using關鍵字,會自動釋放 using (FileStream fs = new FileStream(filePath, FileMode.Open)) { using (StreamReader sw = new StreamReader(fs, Encoding.Default)) { while (!sw.EndOfStream) { string lineInfo = sw.ReadLine(); dic.Add(lineInfo.Split('=')[0], lineInfo.Split('=')[1]); } } } this.txtName.Text = dic["Name"]; this.dtBirthday.Text = dic["Birthday"]; if (dic["Gender"] == this.rbBoy.Text) { this.rbBoy.Checked = true; } else { this.rbGirl.Checked = true; } string[] loves = dic["Love"].Split('|'); foreach (var love in loves) { foreach (var item in this.lsvLove.Items) { ListViewItem li = item as ListViewItem; if (li.Text == love) { li.Checked = true; } } } }
保存和讀取Xml文檔代碼如下:
/// <summary> /// 保存成Xml /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveXml_Click(object sender, EventArgs e) { Person p = GetPersonInfos(); string filePath = "objPerson.xml"; using (FileStream fs = new FileStream(filePath, FileMode.Create)) { XmlSerializer serializer = new XmlSerializer(typeof(Person)); serializer.Serialize(fs, p); } } /// <summary> /// 從Xml中讀取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnReadXml_Click(object sender, EventArgs e) { string filePath = "objPerson.xml"; Person p; using (FileStream fs = new FileStream(filePath, FileMode.Open)) { XmlSerializer serializer = new XmlSerializer(typeof(Person)); object obj= serializer.Deserialize(fs); p = obj as Person; } SetPersonInfos(p); }
保存和讀取Json文檔如下:
/// <summary> /// 保存成Json /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveJson_Click(object sender, EventArgs e) { Person p = GetPersonInfos(); JavaScriptSerializer jserializer = new JavaScriptSerializer(); string strJson = jserializer.Serialize(p); string strRegex = @"\\/Date\((\d+)\)\\/"; MatchEvaluator evaluator = new MatchEvaluator(ConvertJsonDateToDateString); //對時間進行處理,需要引用System.Text.RegularExpressions;命名空間 Regex reg = new Regex(strRegex); strJson = reg.Replace(strJson, evaluator); string filePath = "objPerson.json"; using (FileStream fs = new FileStream(filePath, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { sw.Write(strJson); } } } /// <summary> /// 從Json中讀取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnReadJson_Click(object sender, EventArgs e) { JavaScriptSerializer jserializer = new JavaScriptSerializer(); string filePath = "objPerson.json"; Person p; using (FileStream fs = new FileStream(filePath, FileMode.Open)) { using (StreamReader sw = new StreamReader(fs, Encoding.Default)) { string strJson = sw.ReadToEnd(); string strRegex = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"; MatchEvaluator evaluator = new MatchEvaluator(ConvertDateStringToJsonDate); //對時間進行處理 Regex reg = new Regex(strRegex); strJson = reg.Replace(strJson, evaluator); p = jserializer.Deserialize<Person>(strJson); } } SetPersonInfos(p); }
保存和讀取Bin文檔如下:
/// <summary> /// 保存成二進制文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveBin_Click(object sender, EventArgs e) { Person p = GetPersonInfos(); string filePath = "objPerson.bin"; using (FileStream fs = new FileStream(filePath, FileMode.Create)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, p); } } /// <summary> /// 讀取二進制文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnReadBin_Click(object sender, EventArgs e) { string filePath = "objPerson.bin"; Person p; using (FileStream fs = new FileStream(filePath, FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); p= bf.Deserialize(fs) as Person; } SetPersonInfos(p); }
備注:其實對象持久化和對象序列化是兩個不同的概念。兩者有關聯卻不同。
對象持久化:是使對象可以保存到實體存儲介質中,在對象生命周期結束後可以再現。
對象序列化:是將對象或者數據結構轉化成特定的格式,使其可在網絡中傳輸,或者可存儲在內存或者文件中。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持!