1 import java.io.Serializable;
2
3 /**
4 * <p>ClassName: Person<p>
5 * <p>Description:测试对象序列化和反序列化<p>
6 * @author xudp
7 * @version 1.0 V
8 * @createTime 2014-6-9 下午02:33:25
9 */
10 public class Person implements Serializable {
11
12 /**
13 * 序列化ID
14 */
15 private static final long serialVersionUID = -5809782578272943999L;
16 private int age;
17 private String name;
18 private String sex;
19
20 public int getAge() {
21 return age;
22 }
23
24 public String getName() {
25 return name;
26 }
27
28 public String getSex() {
29 return sex;
30 }
31
32 public void setAge(int age) {
33 this.age = age;
34 }
35
36 public void setName(String name) {
37 this.name = name;
38 }
39
40 public void setSex(String sex) {
41 this.sex = sex;
42 }
43 }
import java.io.File;
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.text.MessageFormat;
/**
* <p>ClassName: TestObjSerializeAndDeserialize<p>
* <p>Description: 测试对象的序列化和反序列<p>
* @author xudp
* @version 1.0 V
* @createTime 2014-6-9 下午03:17:25
*/
public class TestObjSerializeAndDeserialize {
public static void main(String[] args) throws Exception {
SerializePerson();//序列化Person对象
Person p = DeserializePerson();//反序列Perons对象
System.out.println(MessageFormat.format("name={0},age={1},sex={2}",
p.getName(), p.getAge(), p.getSex()));
}
/**
* MethodName: SerializePerson
* Description: 序列化Person对象
* @author xudp
* @throws FileNotFoundException
* @throws IOException
*/
private static void SerializePerson() throws FileNotFoundException,
IOException {
Person person = new Person();
person.setName("gacl");
person.setAge(25);
person.setSex("男");
// ObjectOutputStream 对象输出流,将Person对象存储到E盘的Person.txt文件中,完成对Person对象的序列化操作
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(
new File("E:/Person.txt")));
oo.writeObject(person);
System.out.println("Person对象序列化成功!");
oo.close();
}
/**
* MethodName: DeserializePerson
* Description: 反序列Perons对象
* @author xudp
* @return
* @throws Exception
* @throws IOException
*/
private static Person DeserializePerson() throws Exception, IOException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
new File("E:/Person.txt")));
Person person = (Person) ois.readObject();
System.out.println("Person对象反序列化成功!");
return person;
}
}
1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.ObjectInputStream;
7 import java.io.ObjectOutputStream;
8 import java.io.Serializable;
9
10 public class TestSerialversionUID {
11
12 public static void main(String[] args) throws Exception {
13 SerializeCustomer();// 序列化Customer对象
14 Customer customer = DeserializeCustomer();// 反序列Customer对象
15 System.out.println(customer);
16 }
17
18 /**
19 * MethodName: SerializeCustomer
20 * Description: 序列化Customer对象
21 * @author xudp
22 * @throws FileNotFoundException
23 * @throws IOException
24 */
25 private static void SerializeCustomer() throws FileNotFoundException,
26 IOException {
27 Customer customer = new Customer("gacl",25);
28 // ObjectOutputStream 对象输出流
29 ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(
30 new File("E:/Customer.txt")));
31 oo.writeObject(customer);
32 System.out.println("Customer对象序列化成功!");
33 oo.close();
34 }
35
36 /**
37 * MethodName: DeserializeCustomer
38 * Description: 反序列Customer对象
39 * @author xudp
40 * @return
41 * @throws Exception
42 * @throws IOException
43 */
44 private static Customer DeserializeCustomer() throws Exception, IOException {
45 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
46 new File("E:/Customer.txt")));
47 Customer customer = (Customer) ois.readObject();
48 System.out.println("Customer对象反序列化成功!");
49 return customer;
50 }
51 }
52
53 /**
54 * <p>ClassName: Customer<p>
55 * <p>Description: Customer实现了Serializable接口,可以被序列化<p>
56 * @author xudp
57 * @version 1.0 V
58 * @createTime 2014-6-9 下午04:20:17
59 */
60 class Customer implements Serializable {
61 //Customer类中没有定义serialVersionUID
62 private String name;
63 private int age;
64
65 public Customer(String name, int age) {
66 this.name = name;
67 this.age = age;
68 }
69
70 /*
71 * @MethodName toString
72 * @Description 重写Object类的toString()方法
73 * @author xudp
74 * @return string
75 * @see java.lang.Object#toString()
76 */
77 @Override
78 public String toString() {
79 return "name=" + name + ", age=" + age;
80 }
81 }
1 class Customer implements Serializable {
2 //Customer类中没有定义serialVersionUID
3 private String name;
4 private int age;
5
6 //新添加的sex属性
7 private String sex;
8
9 public Customer(String name, int age) {
10 this.name = name;
11 this.age = age;
12 }
13
14 public Customer(String name, int age,String sex) {
15 this.name = name;
16 this.age = age;
17 this.sex = sex;
18 }
19
20 /*
21 * @MethodName toString
22 * @Description 重写Object类的toString()方法
23 * @author xudp
24 * @return string
25 * @see java.lang.Object#toString()
26 */
27 @Override
28 public String toString() {
29 return "name=" + name + ", age=" + age;
30 }
31 }
1 Exception in thread "main" java.io.InvalidClassException: Customer; 2 local class incompatible: 3 stream classdesc serialVersionUID = -88175599799432325, 4 local class serialVersionUID = -5182532647273106745
class Customer implements Serializable {
/**
* Customer类中定义的serialVersionUID(序列化版本号)
*/
private static final long serialVersionUID = -5182532647273106745L;
private String name;
private int age;
//新添加的sex属性
//private String sex;
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
/*public Customer(String name, int age,String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}*/
/*
* @MethodName toString
* @Description 重写Object类的toString()方法
* @author xudp
* @return string
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "name=" + name + ", age=" + age;
}
}
class Customer implements Serializable {
/**
* Customer类中定义的serialVersionUID(序列化版本号)
*/
private static final long serialVersionUID = -5182532647273106745L;
private String name;
private int age;
//新添加的sex属性
private String sex;
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
public Customer(String name, int age,String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
/*
* @MethodName toString
* @Description 重写Object类的toString()方法
* @author xudp
* @return string
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "name=" + name + ", age=" + age;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有