Completed Tolerant Reader example.
This commit is contained in:
parent
113e1a2f22
commit
b58311aedf
@ -1,7 +1,19 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args ) {
|
||||
import java.io.IOException;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args ) throws IOException, ClassNotFoundException {
|
||||
RainbowFish fishV1 = new RainbowFish("Zed", 10, 11, 12);
|
||||
RainbowFishSerializer.write(fishV1, "fish1.out");
|
||||
RainbowFish deserializedFishV1 = RainbowFishSerializer.read("fish1.out");
|
||||
System.out.println(String.format("deserializedFishV1 name=%s age=%d length=%d weight=%d", deserializedFishV1.getName(),
|
||||
deserializedFishV1.getAge(), deserializedFishV1.getLengthMeters(), deserializedFishV1.getWeightTons()));
|
||||
RainbowFishV2 fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
|
||||
RainbowFishSerializer.write(fishV2, "fish2.out");
|
||||
RainbowFish deserializedFishV2 = RainbowFishSerializer.read("fish2.out");
|
||||
System.out.println(String.format("deserializedFishV2 name=%s age=%d length=%d weight=%d", deserializedFishV2.getName(),
|
||||
deserializedFishV2.getAge(), deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons()));
|
||||
}
|
||||
}
|
||||
|
@ -11,28 +11,27 @@ public class RainbowFish implements Serializable {
|
||||
private int lengthMeters;
|
||||
private int weightTons;
|
||||
|
||||
public RainbowFish(String name, int age, int lengthMeters, int weightTons) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.lengthMeters = lengthMeters;
|
||||
this.weightTons = weightTons;
|
||||
}
|
||||
|
||||
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 int getLengthMeters() {
|
||||
return lengthMeters;
|
||||
}
|
||||
public void setLengthMeters(int lengthMeters) {
|
||||
this.lengthMeters = lengthMeters;
|
||||
}
|
||||
|
||||
public int getWeightTons() {
|
||||
return weightTons;
|
||||
}
|
||||
public void setWeightTons(int weightTons) {
|
||||
this.weightTons = weightTons;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,30 +1,39 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class RainbowFishSerializer {
|
||||
|
||||
public void write(RainbowFish rainbowFish, String filename) {
|
||||
public static void write(RainbowFish 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()));
|
||||
try {
|
||||
FileOutputStream fileOut = new FileOutputStream("fish.ser");
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
||||
objOut.writeObject(map);
|
||||
objOut.close();
|
||||
fileOut.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
||||
objOut.writeObject(map);
|
||||
objOut.close();
|
||||
fileOut.close();
|
||||
}
|
||||
|
||||
// public RainbowFish read(String filename) {
|
||||
// }
|
||||
public static RainbowFish read(String filename) throws IOException, ClassNotFoundException {
|
||||
Map<String, String> map = null;
|
||||
FileInputStream fileIn = new FileInputStream(filename);
|
||||
ObjectInputStream objIn = new ObjectInputStream(fileIn);
|
||||
map = (Map<String, String>) objIn.readObject();
|
||||
objIn.close();
|
||||
fileIn.close();
|
||||
return new RainbowFish(map.get("name"),
|
||||
Integer.parseInt(map.get("age")),
|
||||
Integer.parseInt(map.get("lengthMeters")),
|
||||
Integer.parseInt(map.get("weightTons")));
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,33 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class RainbowFishV2 extends RainbowFish {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private boolean sleeping;
|
||||
private boolean hungry;
|
||||
private boolean angry;
|
||||
|
||||
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons) {
|
||||
super(name, age, lengthMeters, weightTons);
|
||||
}
|
||||
|
||||
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping, boolean hungry, boolean angry) {
|
||||
this(name, age, lengthMeters, weightTons);
|
||||
this.sleeping = sleeping;
|
||||
this.hungry = hungry;
|
||||
this.angry = angry;
|
||||
}
|
||||
|
||||
public boolean getSleeping() {
|
||||
return sleeping;
|
||||
}
|
||||
|
||||
public boolean getHungry() {
|
||||
return hungry;
|
||||
}
|
||||
|
||||
public boolean getAngry() {
|
||||
return angry;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,14 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
public void test() throws ClassNotFoundException, IOException {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user