欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java,C#使用二進(jìn)制序列化、反序列化操作數(shù)據(jù)

 更新時間:2014年10月29日 14:55:56   投稿:hebedich  
這篇文章主要介紹了Java,C#使用二進(jìn)制序列化、反序列化操作數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下

java使用二進(jìn)制序列化、反序列化的操作首先,要引入java.io下面相關(guān)包,或者直接寫import java.io.*;
下面,為了書寫操作的方便,采用復(fù)制文件,和throws聲明異常的方式來寫

復(fù)制代碼 代碼如下:

public void test6() throws IOException {
byte[] b = new byte[1024];//定義字節(jié)數(shù)組,緩沖

FileInputStream in = new FileInputStream("E:\\logo.gif");//創(chuàng)建輸入流對象

FileOutputStream out = new FileOutputStream("E:\\My.gif");//創(chuàng)建輸出流對象

DataInputStream input = new DataInputStream(in);//創(chuàng)建輸入二進(jìn)制流

DataOutputStream dout = new DataOutputStream(out);//創(chuàng)建輸出二進(jìn)制流

int num = input.read(b);// 讀取二進(jìn)制文件到b中
while (num != -1) {
dout.write(b, 0, num);// 將讀取到的數(shù)組寫入到輸出流
num = input.read(b);// 重新再次讀取
}
// 按順序關(guān)閉所有流對象
input.close();
dout.close();
in.close();
out.close();
System.out.println("復(fù)制成功!");
}

初略代碼,僅供參考!
C#使用二進(jìn)制序列化、反序列化的操作首先,引入命名空間using System.Runtime.Serialization.Formatters.Binary;用以操作序列化和反序列化
還有,在牽涉到序列化的自定義類的類上方加上一個指示類[Serializable]
示例:
[Serializable]

復(fù)制代碼 代碼如下:

public class PlayManager
    {
/// <summary>
        /// 序列化保存數(shù)據(jù)
        /// </summary>
        public void Save()
        {
        FileStream fs = null;
            try
            {
                fs = new FileStream("保存文件的路徑", FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, 要保存的對象);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                fs.Close();
            }
/// <summary>
        /// 加載序列化信息
        /// </summary>
        public void Load()
        {
FileStream fs = null;
                try
                {
                    fs = new FileStream("文件路徑", FileMode.OpenOrCreate);
                    BinaryFormatter bf = new BinaryFormatter();
                    對象接收= (對象的類型)bf.Deserialize(fs);   //強(qiáng)制類型轉(zhuǎn)換
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    fs.Close();
                }  

這就是在C#中序列化文件的使用,其實(shí)這個挺簡單的,如果不加try-catch-finally也就四句代碼,
過往的朋友們你們看明白了嗎?不明白的還可以提問哦!

相關(guān)文章

最新評論