1轿亮、序列化疮薇、反序列化接口
/**
* 序列化、反序列化接口
*/
public interface ISerialization {
// 序列化
<T> byte[] serializer(T obj);
// 反序列化
<T> T deSerializer(byte[] data,Class<T> clazz);
}
2我注、實現(xiàn)自己定義的接口
/**
* 序列化 反序列化 操作
* 序列化就是:程序運行必定在內(nèi)存中按咒;
* 從正在運行的程序中,即內(nèi)存中將數(shù)據(jù)但骨;
* 序列化 便于存儲到磁盤(硬盤)中励七,
* 或者便于網(wǎng)絡(luò)傳輸?shù)狡渌麢C器上
* 反序列化:將磁盤(硬盤)中,序列化好的內(nèi)容奔缠,讀取到正在運行的程序中掠抬,即內(nèi)存
*/
public class JavaSerializer implements ISerialization {
@Override
public <T> byte[] serializer(T obj) {
ObjectOutputStream objectOutputStream=null;
try {
objectOutputStream=new ObjectOutputStream(new FileOutputStream(new File("user")));
objectOutputStream.writeObject(obj);
}catch (Exception e){
e.printStackTrace();
}finally {
if(objectOutputStream!=null){
try{
objectOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
return null;
}
@Override
public <T> T deSerializer(byte[] data, Class<T> clazz) {
ObjectInputStream objectInputStream=null;
try {
objectInputStream =new ObjectInputStream(new FileInputStream(new File("user")));
return (T)objectInputStream.readObject();
}catch (Exception e){
e.printStackTrace();
}finally {
if(objectInputStream!=null){
try {
objectInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
return null;
}
}
3、實體類
注意;
1校哎、Serializable必須得實現(xiàn)java自帶的序列化接口
2两波、使用IDEA 自帶的 serialVersionUID ,不能刪除 闷哆;刪除不能發(fā)序列成功
public class User implements Serializable{
// 序列化值 不能刪除 腰奋,如果刪除 ,那么反序列化就不能把文件讀取出去
private static final long serialVersionUID = -2236182038380256247L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
4抱怔、測試
public class Demo {
public static void main(String[] args) {
// ----------注釋start1
ISerialization iSerialization=new JavaSerializer();
User user=new User();
user.setAge(18);
user.setName("Mic");
byte[] serialByte=iSerialization.serializer(user); // 序列化
// ----------注釋end1
// 運行 之后 產(chǎn)生一個user文件
User dUser=iSerialization.deSerializer(serialByte,User.class);// 反序列化
System.out.println(dUser);
}
}
運行結(jié)果
產(chǎn)生一個user文件劣坊;
User{name='Mic', age=18}
我們把User中的 private static final long serialVersionUID = -2236182038380256247L;
注釋掉
然后改寫Demo中的方法
public static void main(String[] args) {
ISerialization iSerialization=new JavaSerializer();
byte[] serialByte= file2byte("E:/MyCode-Gupao/serialization/user");
User dUser=iSerialization.deSerializer(serialByte,User.class);
System.out.println(dUser);
}
// 讀取文件成字節(jié)數(shù)組
public static byte[] file2byte(String path)
{
try {
FileInputStream in =new FileInputStream(new File(path));
//當(dāng)文件沒有結(jié)束時,每次讀取一個字節(jié)顯示
byte[] data=new byte[in.available()];
in.read(data);
in.close();
return data;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
運行之后屈留,報錯
java.io.InvalidClassException: serialization.User; local class incompatible:
stream classdesc serialVersionUID = -2236182038380256247,
local class serialVersionUID = -3132643898329134254
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:687)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1876)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1745)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2033)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
at serialization.JavaSerializer.deSerializer(JavaSerializer.java:104)
at serialization.Demo.main(Demo.java:30)
null
所以如果使用IDEA 的serialVersionUID 不能刪除
我們也可以將上面的第1步保留局冰,其他的都重新改寫
2测蘑、 實現(xiàn)自己定義的接口
public class JavaSerializer implements ISerialization {
@Override
public <T> byte[] serializer(T obj) {
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
public <T> T deSerializer(byte[] data, Class<T> clazz) {
ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(data);
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(byteArrayInputStream);
return (T)objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
3、實體類
public class User implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
4锐想、測試類
public class Demo {
public static void main(String[] args) {
ISerialization iSerialization=new JavaSerializer();
User user=new User();
user.setAge(18);
user.setName("Mic");
byte[] serialByte=iSerialization.serializer(user);
// 運行 之后 產(chǎn)生一個user文件
User dUser=iSerialization.deSerializer(serialByte,User.class);
System.out.println(dUser);
}
運行結(jié)果
User{name='Mic', age=18}