如何将hashmap和图像保存到同一个文件中

时间:2015-07-19 03:55:18

标签: java image serialization hashmap

我想知道如何将HashMap(String,List(Object))保存并加载到文件中.Object类如下所示:

public class FlashCard implements Serializable{
private String question;
private Picture picture;
private ArrayList<String> answers = new ArrayList<>();
private ArrayList<Picture> pictures = new ArrayList<>();

    public FlashCard(){
    }

    // Method should set a list of answers
    public void setAnswers(ArrayList<String> answers){
        this.answers = answers;
    }

    // Method should add an answer to a list of answers
    public void setAnswer(String answer){
    answers.add(answer);
}

    // Method should set a list of answers
    public void setPictures(ArrayList<Picture> pictures){
        this.pictures = pictures;
    }

     // Method should add an answer to a list of answers
public void setPicture(Picture picture){
    pictures.add(picture);
}


public void setQuestion(String question){
    this.question = question;
}

public ArrayList<String> getAnswers(){
    return answers;
}

public ArrayList<Picture> getPictures(){
    return pictures;
}

public String getQuestion(){
    return question;
}
}

图片类从任何图片格式转换为BufferedImage图片。我读到BufferedImage类不可序列化。这是否意味着我无法将它们保存到同一个文件中?谢谢。

1 个答案:

答案 0 :(得分:0)

您应该能够扩展BufferedImage并让该类实现Serializable。像这样:

public class SerializableImage extends BufferedImage implements Serializable{
...
}

然后使用SerializableImage代替BufferedImage,你应该好好去!