JAVA基础对象的序列化与反序列化
2018-01-25 09:23
239 查看
对象类
package com.job.io;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 2517802070568447027L;
private String name;
private int score;
private transient String vanlaentne;
public String getVanlaentne() {
return vanlaentne;
}
public void setVanlaentne(String vanlaentne) {
this.vanlaentne = vanlaentne;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
序列化与反序列化
package com.job.io;
import java.io.*;
import java.security.GeneralSecurityException;
import javax.security.auth.Subject;
import javax.swing.plaf.synth.SynthSeparatorUI;
public class SerializableDemo {
/*
* 对象序列化?将对象的状态持久化(文件,网络)的过程。一般是二进制流。
* 对象反序列化?把二进制还原成对象的过程。
* ObjectOutputStream:从对象转换为流
* 注意:Serializable接口没有任何方法和属性,就叫标记接口。
* ObjectInputStream:流转换为对象
* */
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file = new File("serializable.txt");
//1,创建一个序列化流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("serializable.txt"));
//2,创建一个要被序列化的对象
Student st=new Student();
st.setName("赵云");
st.setScore(99);
st.setVanlaentne("貂蝉");
//3,序列化对象:writeObjec(Object obj) 将指定的对象写入ObjectOutputStream
oos.writeObject(st);
//4,释放资源
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Student obj=(Student)ois.readObject();
ois.close();
System.out.println(obj.getName()+"战力值:"+obj.getScore()+"丨情人:保密"+obj.getVanlaentne());
}
}
package com.job.io;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 2517802070568447027L;
private String name;
private int score;
private transient String vanlaentne;
public String getVanlaentne() {
return vanlaentne;
}
public void setVanlaentne(String vanlaentne) {
this.vanlaentne = vanlaentne;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
序列化与反序列化
package com.job.io;
import java.io.*;
import java.security.GeneralSecurityException;
import javax.security.auth.Subject;
import javax.swing.plaf.synth.SynthSeparatorUI;
public class SerializableDemo {
/*
* 对象序列化?将对象的状态持久化(文件,网络)的过程。一般是二进制流。
* 对象反序列化?把二进制还原成对象的过程。
* ObjectOutputStream:从对象转换为流
* 注意:Serializable接口没有任何方法和属性,就叫标记接口。
* ObjectInputStream:流转换为对象
* */
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file = new File("serializable.txt");
//1,创建一个序列化流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("serializable.txt"));
//2,创建一个要被序列化的对象
Student st=new Student();
st.setName("赵云");
st.setScore(99);
st.setVanlaentne("貂蝉");
//3,序列化对象:writeObjec(Object obj) 将指定的对象写入ObjectOutputStream
oos.writeObject(st);
//4,释放资源
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Student obj=(Student)ois.readObject();
ois.close();
System.out.println(obj.getName()+"战力值:"+obj.getScore()+"丨情人:保密"+obj.getVanlaentne());
}
}
相关文章推荐
- java基础之 对象序列化、transient关键字、StringTokenizer
- Java基础学习总结——Java对象的序列化和反序列化
- Java基础学习总结——Java对象的序列化和反序列化
- java基础——Java对象的序列化与反序列化
- Java基础学习总结——Java对象的序列化和反序列化
- Java基础学习总结——Java对象的序列化和反序列化
- 介绍Java对象序列化使用基础
- java教程之对象序列化使用基础示例详解
- java基础学习---java对象序列化基础知识
- java培训:Java对象序列化使用基础
- Java基础回顾 : 对象序列化和反序列化
- Java基础之对象序列化
- Java基础之序列化对象——将对象写入到文件中(SerializeObjects)
- Java基础学习总结——Java对象的序列化和反序列化
- JAVA对象序列化基础
- Java基础学习总结——Java对象的序列化和反序列化
- JAVA基础学习day22--IO流四-对象序列化、管道流、RandomAccessFile、DataStream、ByteArrayStream、转换流的字符编码
- Java基础学习总结——Java对象的序列化和反序列化