D
D
davespoon2016-02-10 22:34:15
Java
davespoon, 2016-02-10 22:34:15

How to solve serialization problem in java (specific example from head first java)?

Hello! I started learning java from the book Head First Java (Second Edition). And here in chapter 14, file serialization, there is an example program. But the bottom line is that this example does not work, even with sources from the official site. Here is the code itself:
the first file

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

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 axe"});
        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();
        }
    }
}

second file:
package chap14;

import java.io.Serializable;

public class GameCharacter implements Serializable
{
    int power;
    String type;
    String[] weapons;
    
    public GameCharacter(int p, String t, String[] w)
    {
        power = p;
        type = t;
        weapons = w;
    }
    
    public int getPower() {
        return power;
    }
    
    public String getType() {
        return type;
    }
    
    public String getWeapons() {
        String weaponList = "";
        for (int i = 0; i < weapons.length; i++)
        {
            weaponList += weapons[i] + " ";
        }
        return weaponList;
    }
}

Compile errors:
GameSaverTest.java:13: error: cannot find symbol
GameCharacter one = new GameCharacter(50, "Elf", new String[] {"bow", "sword", "dust"});
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:13: error: cannot find symbol
GameCharacter one = new GameCharacter(50, "Elf", new String[] {"bow", "sword", "dust"}) ;
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:14: error: cannot find symbol
GameCharacter two = new GameCharacter(200, "Troll", new String[] {"
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:14: error: cannot find symbol
GameCharacter two = new GameCharacter(200, "Troll", new String[] {"bare hands", "big axe"});
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:15: error: cannot find symbol
GameCharacter three = new GameCharacter(120, "Magician", new String[] {"spells", "invisibility"});
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:15: error: cannot find symbol
GameCharacter three = new GameCharacter(120, "Magician", new String[] {"spells", "invisibility"});
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:34: error: cannot find symbol
GameCharacter oneRestore = (GameCharacter) is.readObject();
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:34: error: cannot find symbol
GameCharacter oneRestore = (GameCharacter) is.readObject();
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:35: error: cannot find symbol
GameCharacter twoRestore = (GameCharacter) is.readObject();
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:35: error: cannot find symbol
GameCharacter twoRestore = (GameCharacter) is.readObject();
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:36: error: cannot find symbol
GameCharacter threeRestore = (GameCharacter) is.readObject();
^
symbol: class GameCharacter
location: class GameSaverTest
GameSaverTest.java:36: error: cannot find symbol
GameCharacter threeRestore = (GameCharacter) is.readObject();
^
symbol: class GameCharacter
location: class GameSaverTest
12 errors
Actually everything...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
davespoon, 2016-02-10
@davespoon

So, I wrote "import java.io.*;" in both files and everything compiled. But here's what is typical - I did this not for the first time, and I'll be a bastard, nothing worked in the previous few. However, maybe I blunted something then, I can. In any case, the problem is solved, thank you all for your participation;)

T
Timur Sergeevich, 2016-02-11
@MyAlesya

Write in development environments!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question