• 串行化与反串行化【并行化】


    应用程序常常需要在硬盘上存储数据,而逐段构建文本和数据文件不是最方便的方式。
    有时最好以对象的形式存储数据。

    ● System.Runtime.Serialization.Formatters.Binary:这个命名空间包含了BinaryFormatter类,它能把对象串行化为二进制数据,把二进制数据串行化为对象。

    ● System.Runtime.Serialization.Formatters.Soap:这个命名空间包含了SoapFormatter类,它能把对象串行化为SOAP格式的XML数据,把SOAP格式的XML数据串行化为对象。

    下面是一个实例,通过这个实例,我们可以就可以很清晰的认识到串行化和并行化的具体处理。

      1using System;
      2using System.Data;
      3using System.Runtime.Serialization;
      4using System.Runtime.Serialization.Formatters.Binary;
      5using System.IO;
      6using System.Text;
      7using System.Collections.Generic;
      8
      9/// <summary>
     10/// Summary description for Teacher
     11/// </summary>

     12[Serializable]//加上这个才能串行化
     13public class Teacher
     14{
     15    /// <summary>
     16    /// 教师ID
     17    /// </summary>

     18    public string ID;
     19    /// <summary>
     20    /// 教师姓名
     21    /// </summary>

     22    public string Name;
     23    /// <summary>
     24    /// 初始化
     25    /// </summary>

     26    public Teacher()
     27    
     28        ///
     29    }

     30    /// <summary>
     31    /// 构造函数
     32    /// </summary>
     33    /// <param name="id"></param>
     34    /// <param name="name"></param>

     35    public Teacher(string id, string name)
     36    {
     37        this.ID = id;
     38        this.Name = name;
     39    }

     40    /// <summary>
     41    /// 串行化对象,保存为文件
     42    /// </summary>
     43    /// <param name="nObject"></param>

     44    public void serializeObject(Teacher nTeacher)
     45    {       
     46        try
     47        {
     48            //BinaryFormatter把对象串行化为二进制数据,把二进制数据串行化为对象
     49            IFormatter formatter = new BinaryFormatter();
     50            //FileStream fileStream = new FileStream(@"d:\teacher22.txt", FileMode.Create,FileAccess.Write);
     51            FileStream fileStream = File.Create(@"d:\data.dat");
     52            formatter.Serialize(fileStream, nTeacher);
     53            fileStream.Close();
     54        }

     55        catch(Exception ex)
     56        {
     57            throw ex;
     58        }

     59        finally
     60        {
     61        }

     62    }

     63    /// <summary>
     64    /// 反串行化||并行化
     65    /// </summar
     66    /// <param name="str"></param>
     67    /// <returns></returns>

     68    public Teacher deserializeObject()
     69    {  
     70        try
     71        
     72            BinaryFormatter derializer=new BinaryFormatter();
     73            Stream stream = new FileStream(@"d:\data.dat", FileMode.Open, FileAccess.Read, FileShare.Read); 
     74
     75            //FileStream nFile = new FileStream(@"d:\teacher22.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
     76            long coutn = stream.Length;
     77            Teacher nTeacher = (Teacher)derializer.Deserialize(stream);
     78            return nTeacher;
     79        }

     80        catch (Exception ex)
     81        {
     82            throw ex;  
     83            return null;
     84        }

     85  
     86    }

     87    /// <summary>
     88    /// 串行化对象
     89    /// </summary>
     90    /// <param name="nObject"></param>

     91    public void serializeObjects(object nObject)
     92    {
     93        FileStream fileStream = new FileStream("d:\path.xml",FileMode.Create);
     94        IFormatter serializer = new BinaryFormatter();
     95        serializer.Serialize(fileStream, nObject);
     96    }

     97    /// <summary>
     98    /// 反串行化---并行化
     99    /// </summary>
    100    /// <param name="str"></param>
    101    /// <returns></returns>

    102    public object deserializeObjects(string strPath)
    103    {
    104        FileStream filestream = new FileStream(@strPath,FileMode.Open);
    105        IFormatter derializer = new BinaryFormatter();
    106        object nObject=(object)derializer.Deserialize(filestream);
    107        return nObject;
    108    }
          
    109}

    110
  • 相关阅读:
    singleTon 模式
    最近的工作经验
    sql server里的快捷键
    Bridge 模式
    [转]在.NET客户端程序中使用多线程
    wse
    关于高频查询界面
    判断字段值已经存在
    获取当前供应商的联系人信息
    获取系统常量
  • 原文地址:https://www.cnblogs.com/yank/p/962007.html
Copyright © 2020-2023  润新知