串行化(序列化)

在MFC中:

CArchive does not have a base class.

The CArchive class allows you to save a complex network of objects in a permanent binary form (usually disk storage) that persists after those objects are deleted. Later you can load the objects from persistent storage, reconstituting them in memory. This process of making data persistent is called “serialization.”

You can think of an archive object as a kind of binary stream. Like an input/output stream, an archive is associated with a file and permits the buffered writing and reading of data to and from storage. An input/output stream processes sequences of ASCII characters, but an archive processes binary object data in an efficient, nonredundant format.

You must create a CFile object before you can create a CArchive object. In addition, you must ensure that the archive’s load/store status is compatible with the file’s open mode. You are limited to one active archive per file.

When you construct a CArchive object, you attach it to an object of class CFile (or a derived class) that represents an open file. You also specify whether the archive will be used for loading or storing. A CArchive object can process not only primitive types but also objects of CObject-derived classes designed for serialization. A serializable class usually has a Serialize member function, and it usually uses the DECLARE_SERIAL and IMPLEMENT_SERIAL macros, as described under class CObject.

The overloaded extraction (>>) and insertion (<<) operators are convenient archive programming interfaces that support both primitive types and CObject-derived classes.

CArchive also supports programming with the MFC Windows Sockets classes CSocket and CSocketFile. The IsBufferEmpty member function supports that usage.

为防止对象在结束生命周期后丢失其信息,可将对象的序列化并保存至磁盘中用于保存对象的信息,一般用于网络传递对象。

CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file,CArchive::store);            //    Saves data to the archive. Requires CFile write permission.
int i=4;
char ch='a';
float f=1.3f;
CString str("write something to ");
ar<<i<<ch<<f<<str;


CFile file("1.txt",CFile::modeRead);
CArchive ar(&file,CArchive::load);
// Loads data from the archive. Requires only CFile read permission
int i;
char ch;
float f;
CString str;
CString strResult;
ar>>i>>ch>>f>>str;
strResult.Format("%d,%c,%f,%s",i,ch,f,str);
MessageBox(strResult
 )


JAVA  中对象需要实现序列化,必须实现Serializable接口

import java.io.Serializable;


public class Person implements Serializable {
private String name;
public Person(String name) {
this.name = name;
}
public String toString() {
return  this.name;
}
}

将对象写入磁盘:

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws Exception {
File file = new File("d:" + File.separator + "person.ser");
ObjectOutputStream oos = null;
oos = new ObjectOutputStream(new FileOutputStream(file));
Person per = new Person("aPerson");
oos.writeObject(per) ;
oos.close() ;
}
}

从磁盘读出对象:

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class ObjectInputStreamDemo {
public static void main(String[] args) throws Exception {
File file = new File("d:" + File.separator + "person.ser");
ObjectInputStream ois = null;
ois = new ObjectInputStream(new FileInputStream(file));
Object obj = ois.readObject();
Person per = (Person) obj;
System.out.println(per);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值