Java中对象的序列化(实现java.io.Serializable)
2012-10-02 12:12
561 查看
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/*对象的寿命随着生成该对象的程序的终止而终止,
* 有时候需要将对象的状态保存下来,在需要的时候再恢复
* 串行化只能保存对象的非静态成员变量
* */
class test implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private Date loggingDate = new Date();
private String uid;
private transient String pwd;//不是串行化的一部分
test(String user,String password){
uid=user;
pwd=password;
}
@Override
public String toString() {
String password= null;
if(pwd==null){
password="Not Set";
}else{
password=pwd;
}
return "logon info:\n"+"user:"+uid+"\n logging date:"+loggingDate.toString()+"\n password:"+password;
}
public static void main(String args[]){
test logInfo = new test("lisi","123456");
System.out.println(logInfo.toString());
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("logInfo.out"));
out.writeObject(logInfo);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("logInfo.out"));
test logInfo1 = (test)in.readObject();
System.out.println(logInfo1.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/*对象的寿命随着生成该对象的程序的终止而终止,
* 有时候需要将对象的状态保存下来,在需要的时候再恢复
* 串行化只能保存对象的非静态成员变量
* */
class test implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private Date loggingDate = new Date();
private String uid;
private transient String pwd;//不是串行化的一部分
test(String user,String password){
uid=user;
pwd=password;
}
@Override
public String toString() {
String password= null;
if(pwd==null){
password="Not Set";
}else{
password=pwd;
}
return "logon info:\n"+"user:"+uid+"\n logging date:"+loggingDate.toString()+"\n password:"+password;
}
public static void main(String args[]){
test logInfo = new test("lisi","123456");
System.out.println(logInfo.toString());
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("logInfo.out"));
out.writeObject(logInfo);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("logInfo.out"));
test logInfo1 = (test)in.readObject();
System.out.println(logInfo1.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- java.io.NotSerializableException: 没实现序列化对象而写入文件流的报错
- java对象序列化java.io.Serializable 接口实践
- java使用serializable进行序列化与反序列化实现对象clone(克隆)
- [Java 12 IO] Serializable 初步 ObjectOutputStream ObjectInputStream 将序列化的对象打出来
- Java IO--对象序列化Serializable、ObjectOutputStream、ObjectInputStream、transient
- 对象序列化(java.io.NotSerializableException: org.json.JSONArray)
- JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码
- JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码
- 在序列化和返序列化深拷贝java对象时出现的 java.io.NotSerializableException
- Android 对象序列化 Java中实现Serializable序列化与Android特有的实现Parceable接口序列化操作
- [Java 12 IO] Serializable 初步 ObjectOutputStream ObjectInputStream 将序列化的对象打出来
- 对象的序列化,需要实现Serializable 接口
- Java利用序列化实现对象深度clone的方法
- java 的对象拷贝(有深浅拷贝两种方式,深拷贝实现的两种方式(逐层实现cloneable接口,序列化的方式来实现))
- Java 序列化对象Serializable 与 Android 序列化对象Parcelable
- Java的对象序列化以及文件IO处理
- (JAVA子类引用指象父类引用)JAVA克隆对象,包括父类属性,无需实现序列化接口
- JAVA 对象序列化小例子实现
- Java IO 对象序列化
- Java学习笔记之IO(十三):对象的序列化和反序列化