package file;
import java.io.*;
// 对象序列化
public class TestWriteObject {
// 实现 Serializable接口
private static class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public static void writeObject() {
Person person = new Person("hike", 27);
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(person);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void readObject() {
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))) {
Person person = (Person) ois.readObject();
System.out.println(person.getName() + "-->" + person.getAge());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
writeObject();
readObject();
}
}
扩展:带有引用变量的对象序列化
引用类型也必须是可以序列化的
package file;
import java.io.*;
// 对象序列化
public class TestWriteObject {
// 实现 Serializable接口
private static class Person implements Serializable {
private String name;
private int age;
// Job 是引用类型,需要把这个引用的类 也序列化才可以
private Job job;
public Person(String name, int age, Job job) {
this.name = name;
this.age = age;
this.job = job;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Job getJob() {
return job;
}
}
private static class Job implements Serializable{
private String job;
public Job(String job) {
this.job = job;
}
public String getJob() {
return job;
}
}
public static void writeObject() {
Person person = new Person("hike", 27, new Job("giser"));
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(person);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void readObject() {
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))) {
Person person = (Person) ois.readObject();
System.out.println(person.getName() + "-->" + person.getAge() + "->" + person.getJob().getJob());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
writeObject();
readObject();
}
}
transient 修饰词 和 类的升级
package file;
import java.io.*;
// 对象序列化
public class TestWriteObject {
// 实现 Serializable接口
private static class Person implements Serializable {
// 解决类升级问题
private static final long serialVersionUID = 2788713780681967735L;
private String name;
// 给字段 使用 transient 修饰词 可以不被序列化, 序列化的时候,获取该字段的时候,获取的就是默认值,比如 int 字段 的默认值 是 0
private transient int age;
// Job 是引用类型,需要把这个引用的类 也序列化才可以
private Job job;
public Person(String name, int age, Job job) {
this.name = name;
this.age = age;
this.job = job;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Job getJob() {
return job;
}
}
private static class Job implements Serializable{
private String job;
public Job(String job) {
this.job = job;
}
public String getJob() {
return job;
}
}
public static void writeObject() {
Person person = new Person("hike", 27, new Job("giser"));
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(person);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void readObject() {
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))) {
Person person = (Person) ois.readObject();
System.out.println(person.getName() + "-->" + person.getAge() + "->" + person.getJob().getJob());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
writeObject();
readObject();
}
}
transient 修饰词 和 类的升级
package file;
import java.io.*;
// 对象序列化
public class TestWriteObject {
// 实现 Serializable接口
private static class Person implements Serializable {
// 解决类升级问题
private static final long serialVersionUID = 2788713780681967735L;
private String name;
// 给字段 使用 transient 修饰词 可以不被序列化, 序列化的时候,获取该字段的时候,获取的就是默认值,比如 int 字段 的默认值 是 0
private transient int age;
// Job 是引用类型,需要把这个引用的类 也序列化才可以
private Job job;
public Person(String name, int age, Job job) {
this.name = name;
this.age = age;
this.job = job;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Job getJob() {
return job;
}
}
private static class Job implements Serializable{
private String job;
public Job(String job) {
this.job = job;
}
public String getJob() {
return job;
}
}
public static void writeObject() {
Person person = new Person("hike", 27, new Job("giser"));
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(person);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void readObject() {
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"))) {
Person person = (Person) ois.readObject();
System.out.println(person.getName() + "-->" + person.getAge() + "->" + person.getJob().getJob());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
writeObject();
readObject();
}
}