Created
December 31, 2016 10:08
-
-
Save alekseytimoshchenko/2a651efb2a15ade4c00b0baa087e2612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Externalizable Person | |
Класс Person должен сериализоваться с помощью интерфейса Externalizable. | |
Подумайте, какие поля не нужно сериализовать. | |
Исправьте ошибку сериализации. | |
Сигнатуры методов менять нельзя. | |
*/ | |
public class Solution { | |
public static void main(String[] args) throws IOException, ClassNotFoundException { | |
Person person = new Person("Ivan", "Ivanov", 23); | |
Person father = new Person("Father", "Father Last Name", 34); | |
Person mother = new Person("Mother", "Mother Last Name", 33); | |
List<Person> children = new ArrayList<>(Arrays.asList( | |
new Person("children name1", "children last name1", 34), | |
new Person("children name 2", " children last name 2", 45) | |
)); | |
person.setFather(father); | |
person.setMother(mother); | |
person.setChildren(children); | |
System.out.println("before : " + person); | |
String path = "/Users/admin/Desktop/properties.txt"; | |
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path)); | |
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path)); | |
oos.writeObject(person); | |
Person deserializablePerson = (Person) ois.readObject(); | |
System.out.println("after : " + deserializablePerson); | |
} | |
public static class Person implements Externalizable { | |
private String firstName; | |
private String lastName; | |
private int age; | |
private Person mother; | |
private Person father; | |
private List<Person> children; | |
public Person() { | |
} | |
public Person(String firstName, String lastName, int age) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.age = age; | |
} | |
public void setMother(Person mother) { | |
this.mother = mother; | |
} | |
public void setFather(Person father) { | |
this.father = father; | |
} | |
public void setChildren(List<Person> children) { | |
this.children = children; | |
} | |
@Override | |
public void writeExternal(ObjectOutput out) throws IOException { | |
out.writeObject(firstName); | |
out.writeObject(lastName); | |
out.writeInt(age); | |
out.writeObject(mother); | |
out.writeObject(father); | |
out.writeObject(children); | |
} | |
@Override | |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { | |
firstName = (String) in.readObject(); | |
lastName = (String) in.readObject(); | |
age = in.readInt(); | |
mother = (Person) in.readObject(); | |
father = (Person) in.readObject(); | |
children = (List) in.readObject(); | |
} | |
@Override | |
public String toString() { | |
return firstName + " " + lastName + " " + age + " " + mother.firstName + " " + | |
father.firstName + " " + children.get(0).firstName + " " + | |
children.get(1).firstName; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment