// 创建写入指定 OutputStream 的 ObjectOutputStream。此构造方法将序列化流部分写入底层流 public ObjectOutputStream(OutputStream out) throws IOException // 将指定的对象写入 ObjectOutputStream public final void writeObject(Object obj) throws IOException
// 创建从指定 InputStream 读取的 ObjectInputStream。 public ObjectInputStream(InputStream in) throws IOException // 从 ObjectInputStream 读取对象。对象的类、类的签名和类及所有其超类型的非瞬态和非静态字段的值都将被读取。 public final Object readObject() throws IOException, ClassNotFoundException
public class Person implements Serializable {
/**
* 序列化ID
*/
private static final long serialVersionUID = 3817849972563375707L;
private String name;
private int age;
private String sex;
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 String getSex() {return sex; }
public void setSex(String sex) {this.sex = sex; }
}
public class TestObjSerializeAndDeserialize {
public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
serializePerson();
Person person = deserializePerson();
System.out.println(MessageFormat.format("name={0},age={1},sex={2}",
person.getName(), person.getAge(), person.getSex()));
}
/**
* 反序列化Person对象
*
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
private static Person deserializePerson() throws FileNotFoundException,
IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
new File("E:\\person.txt")));
Person person = (Person) in.readObject();
System.out.println("反序列化成功!");
return person;
}
/**
* 序列化Person对象
*
* @throws IOException
* @throws FileNotFoundException
*/
private static void serializePerson() throws FileNotFoundException,
IOException {
Person person = new Person();
person.setName("pegasus");
person.setAge(24);
person.setSex("男");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
new File("E:\\person.txt")));
out.writeObject(person);
System.out.println("对象序列化成功!");
out.close();
}
}
public class Person implements Serializable {
private String name;
private int age;
private String sex;
private String address;
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 String getSex() {return sex; }
public void setSex(String sex) {this.sex = sex; }
public String getAddress() {return address; }
public void setAddress(String address) {this.address = address; }
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex
+ ", address=" + address + "]";
}
}
public class DeserializePerson {
public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, IOException {
Person person = deserializePerson();
System.out.println(person);
}
/**
* 反序列化Person对象
*
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
private static Person deserializePerson() throws FileNotFoundException,
IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
new File("E:\\person.txt")));
Person person = (Person) in.readObject();
System.out.println("反序列化成功!");
return person;
}
}
public class Customer implements Serializable{
private static final long serialVersionUID = -4020382581484304699L;
private String name;
private transient String address; // 此属性不被序列化
public Customer(String name, String address) {
this.name = name;
this.address = address;
}
@Override
public String toString() {
return "Customer [name=" + name + ", address=" + address + "]";
}
}
public class TestCustomer {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
serializeCustomer();
deserializeCustomer();
}
private static void deserializeCustomer() throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
new File("E:\\customer.txt")));
Customer customer = (Customer) in.readObject();
System.out.println(customer);
in.close();
}
private static void serializeCustomer() throws FileNotFoundException, IOException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
new File("E:\\customer.txt")));
out.writeObject(new Customer("pegasus", "甘肃"));
System.out.println("序列化成功!");
out.close();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有