标签:二进制 2.0 ora heap please res line mod log
C#提供三种序列化的方法,与此有关的三个类分别是:BinarySerialize、SoapSerialize、XmlSerialize
以二进制的序列化和反序列化为例,先创建一个实体类Product。
重新ToString方法以便打印实体。
给实体赋值,在进行的序列化和反序列化之前进行都进行文件的压缩与解压,用的是GZipStream
List<Product> productList = new List<Product> { new Product(1, "orange", 1.0, "sales"), new Product(2, "Apple", 2.0, "cheap") }; foreach(Product product in productList) { Console.WriteLine(product); } Console.WriteLine("Please select your file"); FileStream fs = new FileStream("Product.ini", FileMode.Create, FileAccess.ReadWrite); GZipStream gzCompressed = new GZipStream(fs, CompressionMode.Compress); try { IFormatter serialize = new BinaryFormatter(); serialize.Serialize(gzCompressed, productList); gzCompressed.Close(); FileStream fsRead = new FileStream("Product.ini", FileMode.Open, FileAccess.Read); GZipStream gzDecompressed = new GZipStream(fsRead, CompressionMode.Decompress); List<Product> deProductList = serialize.Deserialize(gzDecompressed) as List<Product>; gzDecompressed.Close(); foreach (Product product in deProductList) { Console.WriteLine(product); } } catch (SerializationException se) { Console.WriteLine(se.Message); } catch(IOException io) { Console.WriteLine(io.ToString()); }
标签:二进制 2.0 ora heap please res line mod log
原文地址:http://www.cnblogs.com/chenshizhutou/p/6715373.html