vendredi 8 mai 2015

Writeobject to file does not work java

I want to sterilize an object(GameModel) to file and then deserialize that object from that file.

I have UIFClass with a method to get a file name that returns a string. I also have 2 classes fro the serialization part, GameSerializerClass and GameDeserializerClass. Both of them are subclasses of Actionlistener

Here is what is required for the implementation part:

  • gets user selected file through JFileChooser from UIFClass
  • uses GameSerializerClass inside UIFClass which is the actionlistener that serializes (saves) GameModel object into a user specified file.
  • uses GameDeserializerClass which is also an actionlistener that deserializes (readobject) from a user specified file.

I have created a GameModel object and I have a reference inside UIFClass. By the way UIFClass is creating GameSerializerClass object, the constructor is having the (GameModel) object and UIFClass(this). It is also creating GameDeserializerClass object the constructor is having UIFClass(this).

I have a method that will set the actionlistener of JFileChooser to GameSerializerClass and GameDeserializerClass.

inside the actionPerformed I am creating a file object from the string and then pass it to a new ObjectInputStream and then pass the ObjectInputStream to the writeObject(ObjectOutputStream out). inside that method out.writeObject(GameModel).

It does not write to the file, I don't know why!

Here is the code:

public class GameUserInterface {

    public GameSerializer gmserilizer;
    public GameDeserializer gmdeserilizer;
    public GameModel gmMdl;
    public JFileChooser jfc = new  JFileChooser();
    private String saveFileName;
    static JFileChooser fc;
    FileOutputStream fos ;
    FileInputStream fis ;

    public String getsaveFileName()
    {
        jfc.setFileSelectionMode(fc.FILES_ONLY);
        jfc.showOpenDialog(null);
        File myfile = jfc.getSelectedFile();
        saveFileName = myfile.getAbsolutePath();
        System.out.println("the file name is " + saveFileName); 
        return saveFileName; 
    }

void writeTofile()
    {
        getMilestoneMementoManager();

        try {

            jfc.addActionListener(new GameSerializer(this, this.gmMdl));
            fos = new FileOutputStream(getsaveFileName());
            } catch (IOException ex) {
}

    }

GameSerializerClass

public class GameSerializer implements ActionListener {

    private GameUserInterface ui;
    private GameModel model;

    GameSerializer(GameUserInterface ui , GameModel model)
    {
        this.ui = ui;
        this.model = model;
    }

    private void writeObject(ObjectOutputStream obos) throws IOException
    {
        try {
obos.writeObject(model);
           System.out.println("I reached to that point " + model.toString() );
        } catch (IOException ex) {
            Logger.getLogger(GameSerializer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {        
        try {
            System.out.println("I am at actionPerformed in GameSerializer class !");
            File myFile = new File(ui.getsaveFileName());
            ObjectOutputStream obos = new ObjectOutputStream(new FileOutputStream(myFile));
            writeObject(obos);
        } catch (IOException ex) {
            Logger.getLogger(GameDeserializer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

enter image description here

I attached the UML diagram. I am trying to use the snapshot pattern.

Can you please help me? Thank you. Hind

Aucun commentaire:

Enregistrer un commentaire