本文介紹的是除資料庫之外的幾種對象持久化方式。具有很好的參考價值,下面跟著小編一起來看下吧
對象持久化是指將記憶體中的對象儲存到可永久儲存的存放裝置中(如磁碟)的一種技術。
本文介紹的是除資料庫之外的幾種對象持久化方式。
具體如下:
儲存成文本:即將記憶體對象以位元組流的方式儲存到文本中。
序列化成Xml:即將對象以Xml的格式儲存。
序列化成Json:即將對象序列化成Json對象,然後儲存。
序列化成二進位:即將對象序列化成二進位位元組流儲存到檔案中。
涉及知識點:
序列化與還原序列化
檔案流的讀寫
ListView顯示複選框,並橫向排列
如所示【主要功能是將使用者輸入的資訊儲存成各種格式,並從各個文檔中進行讀取出來】:
儲存和讀取文字文件代碼如下:
/// <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); }
備忘:其實對象持久化和對象序列化是兩個不同的概念。兩者有關聯卻不同。
對象持久化:是使對象可以儲存到實體儲存介質中,在對象生命週期結束後可以再現。
對象序列化:是將對象或者資料結構轉化成特定的格式,使其可在網路中傳輸,或者可儲存在記憶體或者檔案中。
以上就是C# 對象持久化圖文代碼詳解的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!