62,620
社区成员
发帖
与我相关
我的任务
分享
public class Person {
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;
}
}
import java.io.IOException;
import java.io.Serializable;
public class Person_Serible extends Person implements Serializable {
private static final long serialVersionUID = 1L;
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();// 先序列化对象
out.writeUTF(super.getName());// 再序列化父类的域
out.writeInt(super.getAge());// 再序列化父类的域
}
private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();// 先反序列化对象
super.setName(in.readUTF());// 再反序列化父类的域
super.setAge(in.readInt());// 再反序列化父类的域
}
}
import java.io.*;
/**
* 测试类
*/
public class Test {
//对person实例序列化
public static void serilizable(Person person, String writePath) throws IOException{
FileOutputStream fileOutputStream = new FileOutputStream(new File(writePath));
ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
outputStream.writeObject(person);
outputStream.close();
}
//反序列化
public static Person unSerilizable(String filePath) throws ClassNotFoundException, IOException{
FileInputStream fileInputStream = new FileInputStream(new File(filePath));
ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
Person person = (Person_Serible) inputStream.readObject();
inputStream.close();
return person;
}
public static void main(String[] args) {
Person person = new Person_Serible();
person.setName("zhangsan");
person.setAge(15);
try {
Test.serilizable(person, "D:\\person.txt");//序列化为磁盘文件
Person person2 = Test.unSerilizable("D:\\person.txt");//从磁盘文件读出来序列化文件
System.out.println(person2.getName() + "\t" + person2.getAge());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
参见:http://www.yesky.com/376/1908876.shtml
JsonMapper.nonEmptyMapper().toJson(object)
JsonMapper.nonEmptyMapper().fromJson(jsonString, clazz)
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson.version}</version>
</dependency>
public class Person_Serible extends Person implements Serializable{}
public class Person_Serible implements Serializable{}
然后使用Person_Serible就可以了
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class SerializableDemo {
public static void main(String[] args) throws Exception {
Person pers = new Person();
pers.setName("Lee");
pers.setAge(20);
pers.setBirthday(new Date());
PersonSerializable perSeri = new PersonSerializable();
perSeri.copy(pers);
ByteArrayOutputStream bou = new ByteArrayOutputStream();
ObjectOutputStream oou = new ObjectOutputStream(bou);
oou.writeObject(perSeri);
ByteArrayInputStream bin = new ByteArrayInputStream(bou.toByteArray());
ObjectInputStream oin = new ObjectInputStream(bin);
PersonSerializable perSeriClone = (PersonSerializable)oin.readObject();
System.out.println(perSeriClone.getName());
System.out.println(perSeriClone.getAge());
System.out.println(perSeriClone.getBirthday());
System.out.println("======================");
Person persClone = perSeriClone.copyToPerson();
System.out.println(persClone.getName());
System.out.println(persClone.getAge());
System.out.println(persClone.getBirthday());
}
}
class PersonSerializable implements Serializable {
private static final long serialVersionUID = -8564152324839732089L;
private String name;
private int age;
private Date birthday;
public void copy(Person person){
if (person == null) return;
this.setName(person.getName());
this.setBirthday(person.getBirthday());
this.setAge(person.getAge());
}
public Person copyToPerson(){
Person person = new Person();
person.setName(this.getName());
person.setBirthday(this.getBirthday());
person.setAge(this.getAge());
return person;
}
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
class Person {
private String name;
private int age;
private Date birthday;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}