Work on serializer.
This commit is contained in:
parent
27a11aa9fc
commit
113e1a2f22
38
tolerant-reader/src/main/java/com/iluwatar/RainbowFish.java
Normal file
38
tolerant-reader/src/main/java/com/iluwatar/RainbowFish.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class RainbowFish implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
private int lengthMeters;
|
||||||
|
private int 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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class RainbowFishSerializer {
|
||||||
|
|
||||||
|
public void write(RainbowFish rainbowFish, String filename) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// public RainbowFish read(String filename) {
|
||||||
|
// }
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class RainbowFishV2 extends RainbowFish {
|
||||||
|
|
||||||
|
private boolean sleeping;
|
||||||
|
private boolean hungry;
|
||||||
|
private boolean angry;
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user