billy.sj
30 Nov 2004, 1:22am
Hi,
I am confused with my java code here. Here are two classes i.e. GameSaver.java and GameCharacter.java
I am experimenting with ObjectOutputStream and ObjectInputStream.
Accordingly, the output when I run GameSaver.java should be
50, Elf, bow, sword, dust
200, Troll, bare hands, big axe
120, Magician, spells, invisibility
But, the output I got when I run the GameSaver.java is:
50, Elf, [Ljava.lang.String;@9304b1
200, Troll, [Ljava.lang.String;@1a758cb
120, Magician, [Ljava.lang.String;@a62fc3
Can anyone explain why? For information, I am running jdk1.5.0 and eclipse IDE version 3.0
The code for both GameSaver.java and GameCharacter.java is listed below:
/*
* Created on 29-Nov-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.billy.Serialize;
import java.io.*;
/**
* @author default
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
class GameSaver{
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 axe"});
GameCharacter three = new GameCharacter(120, "Magician", new String[] {"spells", "invisibility"});
//do things with the characters that might change their state values
//and then save the characters exactly as they are now
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();
}
//now read them back in
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream("Game.ser"));
GameCharacter oneAgain = (GameCharacter) is.readObject();
GameCharacter twoAgain = (GameCharacter) is.readObject();
GameCharacter threeAgain = (GameCharacter) is.readObject();
System.out.println(oneAgain.getPower() +", "+one.getType()+", "+one.getWeapons());
System.out.println(twoAgain.getPower() +", "+two.getType()+", "+two.getWeapons());
System.out.println(threeAgain.getPower() +", "+three.getType()+", "+three.getWeapons());
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
/*
* Created on 29-Nov-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.billy.Serialize;
import java.io.Serializable;
/**
* @author default
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GameCharacter implements Serializable{
private int power;
private String type;
private String[] weapons;
public GameCharacter(int p, String t) {
power = p;
type = t;
}
public GameCharacter(int p, String t, String[] w) {
power = p;
type = t;
weapons = w;
}
public void setPower(int p) {
power = p;
}
public void setType(String t) {
type = t;
}
public void setWeapons(String[] w) {
weapons = w;
}
public int getPower() {
return power;
}
public String getType() {
return type;
}
public String[] getWeapons(){
return weapons;
}
}
I am confused with my java code here. Here are two classes i.e. GameSaver.java and GameCharacter.java
I am experimenting with ObjectOutputStream and ObjectInputStream.
Accordingly, the output when I run GameSaver.java should be
50, Elf, bow, sword, dust
200, Troll, bare hands, big axe
120, Magician, spells, invisibility
But, the output I got when I run the GameSaver.java is:
50, Elf, [Ljava.lang.String;@9304b1
200, Troll, [Ljava.lang.String;@1a758cb
120, Magician, [Ljava.lang.String;@a62fc3
Can anyone explain why? For information, I am running jdk1.5.0 and eclipse IDE version 3.0
The code for both GameSaver.java and GameCharacter.java is listed below:
/*
* Created on 29-Nov-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.billy.Serialize;
import java.io.*;
/**
* @author default
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
class GameSaver{
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 axe"});
GameCharacter three = new GameCharacter(120, "Magician", new String[] {"spells", "invisibility"});
//do things with the characters that might change their state values
//and then save the characters exactly as they are now
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();
}
//now read them back in
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream("Game.ser"));
GameCharacter oneAgain = (GameCharacter) is.readObject();
GameCharacter twoAgain = (GameCharacter) is.readObject();
GameCharacter threeAgain = (GameCharacter) is.readObject();
System.out.println(oneAgain.getPower() +", "+one.getType()+", "+one.getWeapons());
System.out.println(twoAgain.getPower() +", "+two.getType()+", "+two.getWeapons());
System.out.println(threeAgain.getPower() +", "+three.getType()+", "+three.getWeapons());
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
/*
* Created on 29-Nov-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.billy.Serialize;
import java.io.Serializable;
/**
* @author default
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GameCharacter implements Serializable{
private int power;
private String type;
private String[] weapons;
public GameCharacter(int p, String t) {
power = p;
type = t;
}
public GameCharacter(int p, String t, String[] w) {
power = p;
type = t;
weapons = w;
}
public void setPower(int p) {
power = p;
}
public void setType(String t) {
type = t;
}
public void setWeapons(String[] w) {
weapons = w;
}
public int getPower() {
return power;
}
public String getType() {
return type;
}
public String[] getWeapons(){
return weapons;
}
}