1700439010
1700439011
Person person=new Person();
1700439012
1700439013
person.setName(“混世魔王”);
1700439014
1700439015
//序列化,保存到磁盘上
1700439016
1700439017
SerializationUtils.writeObject(person);
1700439018
1700439019
}
1700439020
1700439021
}
1700439022
1700439023
这里引入了一个工具类SerializationUtils,其作用是对一个类进行序列化和反序列化,并存储到硬盘上(模拟网络传输),其代码如下:
1700439024
1700439025
public class SerializationUtils{
1700439026
1700439027
private static String FILE_NAME=“c:/obj.bin”;
1700439028
1700439029
//序列化
1700439030
1700439031
public static void writeObject(Serializable s){
1700439032
1700439033
try{
1700439034
1700439035
ObjectOutputStream oos=new ObjectOutputStream(new
1700439036
1700439037
FileOutputStream(FILE_NAME));
1700439038
1700439039
oos.writeObject(s);
1700439040
1700439041
oos.close();
1700439042
1700439043
}catch(Exception e){
1700439044
1700439045
e.printStackTrace();
1700439046
1700439047
}
1700439048
1700439049
}
1700439050
1700439051
public static Object readObject(){
1700439052
1700439053
Object obj=null;
1700439054
1700439055
//反序列化
1700439056
1700439057
try{
1700439058
1700439059
Object Input in put=new Object Input Stream(new
[
上一页 ]
[ :1.70043901e+09 ]
[
下一页 ]