最近在研究跨进程通信的问题,于是又再一次研究了,我们熟悉而又陌生的Serializable接口。
那么好,做过Java开发的朋友肯定对这个接口不陌生吧,Java中就是通过这个接口,来实现了序列化和反序列化的,那么下面看看使用Serializable接口的小例子。
- public class User implements Serializable{
- private String name;
- private int age;
- private float height;
- 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 float getHeight() {
- return height;
- }
- public void setHeight(float height) {
- this.height = height;
- }
- @Override
- public String toString() {
- return "User [name=" + name + ", age=" + age + ", height=" + height + "]";
- }
- }
public class User implements Serializable{ private String name; private int age; private float height; 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 float getHeight() { return height; } public void setHeight(float height) { this.height = height; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", height=" + height + "]"; } }
上面是我们常见写法的bean,不过呢,Eclipse就会有这个提示:The serializable class User does not declare a static final serialVersionUID field of type long,意思就是说让你添加一个serialVersionUID的值。
不过呢,我们就算不添加,那么也是可以正常序列化的。下面贴一下序列化和反序列化的代码。
- public class Test {
- private static String FILE_PATH = "cache.txt";
- /**
- * 测试SerialVersionUID和不同是否能发序列化成功
- *
- * @param args
- */
- public static void main(String[] args) {
- // 创建一个对象
- User user = new User();
- user.setAge(10);
- user.setName("小可爱");
- user.setHeight(175.6f);
- // 开始序列化
- ObjToFile(user);
- // 开始反序列化
- Object o = FileToObj();
- System.out.println("=====================");
- System.out.println(o.toString());
- }
- /**
- * 序列化
- */<del>
- </del> public static void ObjToFile(Serializable s) {
- ObjectOutputStream _Oos = null;
- try {
- _Oos = new ObjectOutputStream(new FileOutputStream(FILE_PATH));
- _Oos.writeObject(s);
- _Oos.flush();
- System.out.println("序列化成功!");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (_Oos != null) {
- try {
- _Oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 反序列化
- */
- public static Serializable FileToObj() {
- ObjectInputStream _Ois = null;
- try {
- _Ois = new ObjectInputStream(new FileInputStream(FILE_PATH));
- Serializable s = (Serializable) _Ois.readObject();
- System.out.println("反序列化成功");
- return s;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- if (_Ois != null) {
- try {
- _Ois.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return null;
- }
- }
public class Test { private static String FILE_PATH = "cache.txt"; /** * 测试SerialVersionUID和不同是否能发序列化成功 * * @param args */ public static void main(String[] args) { // 创建一个对象 User user = new User(); user.setAge(10); user.setName("小可爱"); user.setHeight(175.6f); // 开始序列化 ObjToFile(user); // 开始反序列化 Object o = FileToObj(); System.out.println("====================="); System.out.println(o.toString()); } /** * 序列化 */先说第一种情况:public static void ObjToFile(Serializable s) { ObjectOutputStream _Oos = null; try { _Oos = new ObjectOutputStream(new FileOutputStream(FILE_PATH)); _Oos.writeObject(s); _Oos.flush(); System.out.println("序列化成功!"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (_Oos != null) { try { _Oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 反序列化 */ public static Serializable FileToObj() { ObjectInputStream _Ois = null; try { _Ois = new ObjectInputStream(new FileInputStream(FILE_PATH)); Serializable s = (Serializable) _Ois.readObject(); System.out.println("反序列化成功"); return s; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (_Ois != null) { try { _Ois.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }}
其实,如果你不添加serialVersionUID,那么Eclipse在会根据这个类的结构(成员变量,成员变量的个数等),生成一个hash值,然后将这个值作为serialVersionUID。
告诉大家实验步骤:
1、序列化User对象的时候,不要添加serialVersionUID。
2、然后给User对象添加serialVersionUID = 2L,可以随便是个long值。
3、开始对新添加的User对象,进行反序列化。
接着大家就可以看到控制台打印的报错了。
我这边的报错是这样的:
java.io.InvalidClassException: com.peng.test.User; local class incompatible: stream classdesc serialVersionUID = -2110639933710420778, local class serialVersionUID = 2