序列化向量 [英] Serializing a vector

查看:170
本文介绍了序列化向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我要储存的游戏是:




  • A char [] [] (二维数组/矩阵)

  • ArrayList< Entity>



Entity是< c $ c> Dragon , Hero 项目。所有这三种类型可以一次包含在 ArrayList



到目前为止我有这样: p>

 包逻辑; 

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

public final class LoadAndSave {
public static final transient boolean available = false;

public static final boolean serialize(Object obj){

//用FileOutputStream写入磁盘
FileOutputStream saveFile;
try {
saveFile = new FileOutputStream(game.sav);
} catch(FileNotFoundException e){
return false;
}

//用ObjectOutputStream写对象
ObjectOutputStream objOut;
try {
objOut = new ObjectOutputStream(saveFile);
} catch(IOException e){
//
return false;
}

//将对象写入磁盘
try {
objOut.writeObject(obj);
} catch(IOException e){
return false;
}

return true;
}

public static final Object load(){
FileInputStream fileIn;
try {
fileIn = new FileInputStream(game.sav);
} catch(FileNotFoundException e1){
return null;
}

//使用ObjectInputStream读取对象
ObjectInputStream objIn;
try {
objIn = new ObjectInputStream(fileIn);
} catch(IOException e){
return null;
}

//读取对象
Object obj;
try {
obj = objIn.readObject();
} catch(IOException e){
return null;
} catch(ClassNotFoundException e){
return null;
}

return obj;
}

}

-explanatory。现在提出我的问题:


  1. 这段代码是否足够?

  2. 我需要为 Dragon 项目 Hero

  3. 序列化机制如何处理我有一个实体向量充满了不是实体的类型,但派生类?
    感谢您的时间!






,除了 ArrayList 之一。当调用 size()时,它不会保存或加载(空指针异常)。



这可能是由什么原因造成的?

解决方案

整个结构是可序列化的,那么你将需要使部件序列化。在实践中这意味着你需要让你的Dragon,Hero和Item类实现Serializable。它只是一个标记接口,所以你只需要添加 implements Serializable 。有些ides(至少eclipse,反正)可能会抱怨你应该添加一个私有长SerialVersionUID 这可能或不可能优化序列化一点 - 在任何情况下,它不是严格必要。



这个需求是递归的 - 它们的任何子部分是自定义或非可序列化的对象也必须被处理,所以例如如果你的Item类看起来像这样

  public class Item implements Serializable {
private String s;
private MyClass c;
..

那么你还需要使MyClass Serializable,变量等。


I'm trying to implement loading and saving for a game I'm working on.

What I want to save is:

  • A char[][] (bidimensional array/matrix)
  • An ArrayList<Entity>

Entity is a super class for Dragon, Hero and Item. All three of these types can be contained at once in the ArrayList.

So far I have this:

package logic;

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

public final class LoadAndSave {
    public static final transient boolean available = false;

    public static final boolean serialize(Object obj) {

        // Write to disk with FileOutputStream
        FileOutputStream saveFile;
        try {
            saveFile = new FileOutputStream("game.sav");
        } catch (FileNotFoundException e) {
            return false;
        }

        // Write object with ObjectOutputStream
        ObjectOutputStream objOut;
        try {
            objOut = new ObjectOutputStream(saveFile);
        } catch (IOException e) {
            //
            return false;
        }

        // Write object out to disk
        try {
            objOut.writeObject(obj);
        } catch (IOException e) {
            return false;
        }

        return true;
    }

    public static final Object load() {
        FileInputStream fileIn;
        try {
            fileIn = new FileInputStream("game.sav");
        } catch (FileNotFoundException e1) {
            return null;
        }

        // Read object using ObjectInputStream
        ObjectInputStream objIn;
        try {
            objIn = new ObjectInputStream(fileIn);
        } catch (IOException e) {
            return null;
        }

        // Read an object
        Object obj;
        try {
            obj = objIn.readObject();
        } catch (IOException e) {
            return null;
        } catch (ClassNotFoundException e) {
            return null;
        }

        return obj;
    }

}

I think the code is pretty self-explanatory. Now for my questions:

  1. Will this code suffice?
  2. Do I need to implement specific serialization methods for Dragon, Item and Hero?
  3. How will the serialization mechanism deal with the fact that I have an Entity vector full of types that are not Entity, but derived classes? Thanks for your time!


OK, all seems to be well, except for one the ArrayList. It is either not getting saved or loaded (null pointer exception when calling size()).

What may this be due to?

解决方案

If you want the entire structure to be serializable, then you'll need to make the parts serialiable as well. What this means in practice is that you need to make your Dragon, Hero, and Item classes implement Serializable. It's just a marker interface, so you only need to add implements Serializable. Some ides (at least eclipse, anyway) may complain that you should add a private long SerialVersionUID which may or may not optimize the serialization a bit - in any case it's not strictly necessary.

This requirement is recursive - any of their subparts that are custom or non-serializable objects have to be taken care of as well, so for example if your Item class looks like this

 public class Item implements Serializable {
    private String s;
    private MyClass c;
    ..

then you'll also need to make MyClass Serializable, any of it's instance variables, etc etc.

这篇关于序列化向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆