-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameSaverTest.java
More file actions
36 lines (31 loc) · 1.17 KB
/
GameSaverTest.java
File metadata and controls
36 lines (31 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.*;
public class GameSaverTest {
public static void main(String[] args) {
GameCharacter one = new GameCharacter(50, "Elf", new String[] {"bow", "sword", "dust"});
GameCharacter two = new GameCharacter(200, "Troll", new String[] {"bare hands", "big ax"});
GameCharacter three = new GameCharacter(120, "Magician", new String[] {"spells", "invisibility"});
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Game.ser"));
os.writeObject(one);
os.writeObject(two);
os.writeObject(three);
os.close();
} catch (IOException ex) {
ex.printStackTrace();
}
one = null;
two = null;
three = null;
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream("Game.ser"));
GameCharacter oneRestore = (GameCharacter) is.readObject();
GameCharacter twoRestore = (GameCharacter) is.readObject();
GameCharacter threeRestore = (GameCharacter) is.readObject();
System.out.println("One's type: " + oneRestore.getType());
System.out.println("Two's type: " + twoRestore.getType());
System.out.println("Three's type: " + threeRestore.getType());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}