Completed Tolerant Reader example.
This commit is contained in:
@ -1,7 +1,19 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
public class App
|
import java.io.IOException;
|
||||||
{
|
|
||||||
public static void main( String[] args ) {
|
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 lengthMeters;
|
||||||
private int weightTons;
|
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() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
public int getAge() {
|
public int getAge() {
|
||||||
return age;
|
return age;
|
||||||
}
|
}
|
||||||
public void setAge(int age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
public int getLengthMeters() {
|
public int getLengthMeters() {
|
||||||
return lengthMeters;
|
return lengthMeters;
|
||||||
}
|
}
|
||||||
public void setLengthMeters(int lengthMeters) {
|
|
||||||
this.lengthMeters = lengthMeters;
|
|
||||||
}
|
|
||||||
public int getWeightTons() {
|
public int getWeightTons() {
|
||||||
return weightTons;
|
return weightTons;
|
||||||
}
|
}
|
||||||
public void setWeightTons(int weightTons) {
|
|
||||||
this.weightTons = weightTons;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,39 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class RainbowFishSerializer {
|
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<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()));
|
||||||
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
|
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
|
||||||
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
|
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
|
||||||
try {
|
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||||
FileOutputStream fileOut = new FileOutputStream("fish.ser");
|
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
||||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
objOut.writeObject(map);
|
||||||
objOut.writeObject(map);
|
objOut.close();
|
||||||
objOut.close();
|
fileOut.close();
|
||||||
fileOut.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
package com.iluwatar;
|
||||||
|
|
||||||
public class RainbowFishV2 extends RainbowFish {
|
public class RainbowFishV2 extends RainbowFish {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private boolean sleeping;
|
private boolean sleeping;
|
||||||
private boolean hungry;
|
private boolean hungry;
|
||||||
private boolean angry;
|
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;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() throws ClassNotFoundException, IOException {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user