Example improvements.
This commit is contained in:
parent
5562795a5f
commit
b3bf4379a4
@ -2,17 +2,30 @@ package com.iluwatar;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main( String[] args ) throws IOException, ClassNotFoundException {
|
public static void main( String[] args ) throws IOException, ClassNotFoundException {
|
||||||
|
// Write V1
|
||||||
RainbowFish fishV1 = new RainbowFish("Zed", 10, 11, 12);
|
RainbowFish fishV1 = new RainbowFish("Zed", 10, 11, 12);
|
||||||
RainbowFishSerializer.write(fishV1, "fish1.out");
|
System.out.println(String.format("fishV1 name=%s age=%d length=%d weight=%d", fishV1.getName(),
|
||||||
RainbowFish deserializedFishV1 = RainbowFishSerializer.read("fish1.out");
|
fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons()));
|
||||||
|
RainbowFishSerializer.writeV1(fishV1, "fish1.out");
|
||||||
|
// Read V1
|
||||||
|
RainbowFish deserializedFishV1 = RainbowFishSerializer.readV1("fish1.out");
|
||||||
System.out.println(String.format("deserializedFishV1 name=%s age=%d length=%d weight=%d", deserializedFishV1.getName(),
|
System.out.println(String.format("deserializedFishV1 name=%s age=%d length=%d weight=%d", deserializedFishV1.getName(),
|
||||||
deserializedFishV1.getAge(), deserializedFishV1.getLengthMeters(), deserializedFishV1.getWeightTons()));
|
deserializedFishV1.getAge(), deserializedFishV1.getLengthMeters(), deserializedFishV1.getWeightTons()));
|
||||||
|
// Write V2
|
||||||
RainbowFishV2 fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
|
RainbowFishV2 fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
|
||||||
RainbowFishSerializer.write(fishV2, "fish2.out");
|
System.out.println(String.format("fishV2 name=%s age=%d length=%d weight=%d sleeping=%b hungry=%b angry=%b", fishV2.getName(),
|
||||||
RainbowFish deserializedFishV2 = RainbowFishSerializer.read("fish2.out");
|
fishV2.getAge(), fishV2.getLengthMeters(), fishV2.getWeightTons(), fishV2.getHungry(), fishV2.getAngry(), fishV2.getSleeping()));
|
||||||
|
RainbowFishSerializer.writeV2(fishV2, "fish2.out");
|
||||||
|
// Read V2 with V1 method
|
||||||
|
RainbowFish deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out");
|
||||||
System.out.println(String.format("deserializedFishV2 name=%s age=%d length=%d weight=%d", deserializedFishV2.getName(),
|
System.out.println(String.format("deserializedFishV2 name=%s age=%d length=%d weight=%d", deserializedFishV2.getName(),
|
||||||
deserializedFishV2.getAge(), deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons()));
|
deserializedFishV2.getAge(), deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons()));
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class RainbowFishSerializer {
|
public class RainbowFishSerializer {
|
||||||
|
|
||||||
public static void write(RainbowFish rainbowFish, String filename) throws IOException {
|
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
|
||||||
Map<String, String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("name", rainbowFish.getName());
|
map.put("name", rainbowFish.getName());
|
||||||
map.put("age", String.format("%d", rainbowFish.getAge()));
|
map.put("age", String.format("%d", rainbowFish.getAge()));
|
||||||
@ -23,8 +23,24 @@ public class RainbowFishSerializer {
|
|||||||
objOut.close();
|
objOut.close();
|
||||||
fileOut.close();
|
fileOut.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
map.put("name", rainbowFish.getName());
|
||||||
|
map.put("age", String.format("%d", rainbowFish.getAge()));
|
||||||
|
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
|
||||||
|
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
|
||||||
|
map.put("angry", Boolean.toString(rainbowFish.getAngry()));
|
||||||
|
map.put("hungry", Boolean.toString(rainbowFish.getHungry()));
|
||||||
|
map.put("sleeping", Boolean.toString(rainbowFish.getSleeping()));
|
||||||
|
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||||
|
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
||||||
|
objOut.writeObject(map);
|
||||||
|
objOut.close();
|
||||||
|
fileOut.close();
|
||||||
|
}
|
||||||
|
|
||||||
public static RainbowFish read(String filename) throws IOException, ClassNotFoundException {
|
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
|
||||||
Map<String, String> map = null;
|
Map<String, String> map = null;
|
||||||
FileInputStream fileIn = new FileInputStream(filename);
|
FileInputStream fileIn = new FileInputStream(filename);
|
||||||
ObjectInputStream objIn = new ObjectInputStream(fileIn);
|
ObjectInputStream objIn = new ObjectInputStream(fileIn);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user