串行化技术
来源:互联网 发布:linux内核的编译与移植 编辑:程序博客网 时间:2024/04/30 19:49
串行化(serialization)是指将一个对象的当前状态转换成字节流(a stream of bytes)的过程,
而反串行化(deserialization)则指串行化过程的逆过程。
串行化也叫持久化。原因是串行化后的字节流可以保存在文件中,使其不会发生数据丢失。
而且它的读取也很容易,只要进行反串行化就可以恢复原来的数据。
在.net上有个专门的用来串行化对象的方法
下面引用MSDN下的一个串行化和反串行化的例子
using System;using System.IO;using System.Collections;using System.Runtime.Serialization.Formatters.Binary;using System.Runtime.Serialization;public class App { [STAThread] static void Main() { Serialize(); Deserialize(); } static void Serialize() { // Create a hashtable of values that will eventually be serialized. Hashtable addresses = new Hashtable(); addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052"); addresses.Add("Fred", "987 Pine Road, Phila., PA 19116"); addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301"); // To serialize the hashtable and its key/value pairs, // you must first open a stream for writing. // In this case, use a file stream. FileStream fs = new FileStream("DataFile.dat", FileMode.Create); // Construct a BinaryFormatter and use it to serialize the data to the stream. BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(fs, addresses); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } finally { fs.Close(); } } static void Deserialize() { // Declare the hashtable reference. Hashtable addresses = null; // Open the file containing the data that you want to deserialize. FileStream fs = new FileStream("DataFile.dat", FileMode.Open); try { BinaryFormatter formatter = new BinaryFormatter(); // Deserialize the hashtable from the file and // assign the reference to the local variable. addresses = (Hashtable) formatter.Deserialize(fs); } catch (SerializationException e) { Console.WriteLine("Failed to deserialize. Reason: " + e.Message); throw; } finally { fs.Close(); } // To prove that the table deserialized correctly, // display the key/value pairs. foreach (DictionaryEntry de in addresses) { Console.WriteLine("{0} lives at {1}.", de.Key, de.Value); } }}
- 串行化技术
- java串行化技术
- Java 串行化技术_Serializable
- usb 串行技术!
- 串行通信技术SERDES
- C#中对象串行化技术的探讨
- 串行化
- 串行化
- 串行化
- 串行化
- 串行化
- 串行化
- 串行化
- Java串行端口技术协议
- Java串行端口技术协议
- Java串行端口通讯技术
- Java串行端口技术协议
- Java串行端口技术协议
- 关于使用phpadmin链接表的额外特性尚未激活的解决办法
- 关闭数据库连接 即关闭 与之相关的所有资源
- 做人的涵养
- 父亲的板栗
- 浅谈Java的输入输出流(转)
- 串行化技术
- 电脑读取系统碟时常见报错信息及解决方案
- 幻想
- TagLib简介
- cab框架扩展UI元素的原理
- Java正则表达式入门
- (二十)午夜迷情 - 1
- 程序员创业致富历程
- 计算机专业英语词汇