Created
February 10, 2016 10:25
-
-
Save FaAway/72cc2542b8d0ac322593 to your computer and use it in GitHub Desktop.
javarush level20.lesson10.home05
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
package com.javarush.test.level20.lesson10.home05; | |
import java.io.*; | |
import java.util.logging.Logger; | |
/* Сериализуйте Person | |
Сериализуйте класс Person стандартным способом. При необходимости поставьте полям модификатор transient. | |
*/ | |
public class Solution { | |
public static void main(String[] args) throws IOException, ClassNotFoundException { | |
Solution solution = new Solution(); | |
Person b = new Person("Name", "Last_Name", "Russia", Sex.MALE); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ObjectOutputStream out = new ObjectOutputStream(baos); | |
out.writeObject(b); | |
out.flush(); | |
out.close(); | |
Person b2 = (Person) (new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))).readObject(); | |
} | |
public static class Person implements Serializable{ | |
String firstName; | |
String lastName; | |
transient String fullName; | |
final transient String greetingString; | |
String country; | |
Sex sex; | |
transient PrintStream outputStream; | |
transient Logger logger; | |
Person(String firstName, String lastName, String country, Sex sex) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.fullName = String.format("%s, %s", lastName, firstName); | |
this.greetingString = "Hello, "; | |
this.country = country; | |
this.sex = sex; | |
this.outputStream = System.out; | |
this.logger = Logger.getLogger(String.valueOf(Person.class)); | |
} | |
} | |
enum Sex { | |
MALE, | |
FEMALE | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment