OnJava8-Examples/io/MyWorld.java
Bruce Eckel 49edcc8b17 reorg
2015-06-15 17:47:35 -07:00

73 lines
2.3 KiB
Java

//: io/MyWorld.java
// ©2015 MindView LLC: see Copyright.txt
import java.io.*;
import java.util.*;
import static com.mindviewinc.util.Print.*;
class House implements Serializable {}
class Animal implements Serializable {
private String name;
private House preferredHouse;
Animal(String nm, House h) {
name = nm;
preferredHouse = h;
}
@Override
public String toString() {
return name + "[" + super.toString() +
"], " + preferredHouse + "\n";
}
}
public class MyWorld {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
House house = new House();
List<Animal> animals = new ArrayList<>();
animals.add(new Animal("Bosco the dog", house));
animals.add(new Animal("Ralph the hamster", house));
animals.add(new Animal("Molly the cat", house));
print("animals: " + animals);
ByteArrayOutputStream buf1 =
new ByteArrayOutputStream();
ObjectOutputStream o1 = new ObjectOutputStream(buf1);
o1.writeObject(animals);
o1.writeObject(animals); // Write a 2nd set
// Write to a different stream:
ByteArrayOutputStream buf2 =
new ByteArrayOutputStream();
ObjectOutputStream o2 = new ObjectOutputStream(buf2);
o2.writeObject(animals);
// Now get them back:
ObjectInputStream in1 = new ObjectInputStream(
new ByteArrayInputStream(buf1.toByteArray()));
ObjectInputStream in2 = new ObjectInputStream(
new ByteArrayInputStream(buf2.toByteArray()));
List
animals1 = (List)in1.readObject(),
animals2 = (List)in1.readObject(),
animals3 = (List)in2.readObject();
print("animals1: " + animals1);
print("animals2: " + animals2);
print("animals3: " + animals3);
}
} /* Output:
animals: [Bosco the dog[Animal@19e0bfd], House@139a55
, Ralph the hamster[Animal@1db9742], House@139a55
, Molly the cat[Animal@106d69c], House@139a55
]
animals1: [Bosco the dog[Animal@10bedb4], House@103dbd3
, Ralph the hamster[Animal@167cf4d], House@103dbd3
, Molly the cat[Animal@a987ac], House@103dbd3
]
animals2: [Bosco the dog[Animal@10bedb4], House@103dbd3
, Ralph the hamster[Animal@167cf4d], House@103dbd3
, Molly the cat[Animal@a987ac], House@103dbd3
]
animals3: [Bosco the dog[Animal@a3a380], House@1453f44
, Ralph the hamster[Animal@ad8086], House@1453f44
, Molly the cat[Animal@be858], House@1453f44
]
*///:~