1

The idea is about serialize this box code. The program is to build flashcard and I want use serializetion to saving the state of the flashapp boxes, when the application exits into a file, and load the file on startup. So I don't have to redo all the flashcard from the beginning. The box code is as follow:

import java.util.ArrayList;
import java.util.Iterator;



public class Box implements java.io.Serializable {
private ArrayList<Flashcard> cards;
private double prossibility;
private double pivot;   //determine the box's selected scope [pivot -          prossibility,pivot)
public Box(){
    cards = new ArrayList<Flashcard>();
    prossibility = 0.0;
    pivot = 0.0;
}
public Box(double prossibility,double pivot){
    this.cards = new ArrayList<Flashcard>(); 
    this.prossibility = prossibility;
    this.pivot = pivot;
}
public Box(ArrayList<Flashcard> cards){
    this.cards = cards;
}


public ArrayList<Flashcard> getCards() {
    return cards;
}

public void setProssibility(double prossibility){
    this.prossibility = prossibility;
}
public double getProssibility(){
    return prossibility;
}

public void setPivot(double pivot){
    this.pivot = pivot;
}
public double getPivot(){
    return pivot;
}

public void addCard(Flashcard card){
    cards.add(card);
}
public int searchCard(String challenge){
    Iterator<Flashcard> it = cards.iterator();
    int index = 0;
    while(it.hasNext()){
        Flashcard temp = (Flashcard)it.next();
        if(temp.getChallenge().equals(challenge)){
            break;
        }
        ++index; 
    }
    if(index >= cards.size())
        index = -1;
    return index;
}
public void removeCard(String challenge){
    int index = searchCard(challenge);
    if(index >= 0)
        cards.remove(index);
}

}

My flashcard class is like:

enum ESide{
FRONT,  //challenge side
BACK    //response side
};
public class Flashcard implements Cloneable{
private String challenge;
private String response;
private ESide side;
private int boxIndex;
public Flashcard(){
    challenge = new String();
    response = new String();
    side = ESide.BACK;
    boxIndex = 0;
}
public Flashcard(String challenge, String response, ESide side){
    this.challenge = challenge;
    this.response = response;
    this.side = side;
    this.boxIndex = 0;
}
public Flashcard(String challenge, String response){
    this.challenge = challenge;
    this.response = response;
    this.side = ESide.BACK;
    this.boxIndex = 0;
}


public void setChallenge(String challenge){
    this.challenge = challenge;
}
public String getChallenge(){
    return challenge;
}

public void setResponse(String response){
    this.response = response;
}
public String getResponse(){
    return response;
}

public void setSide(ESide side){
    this.side = side;
}
public ESide getSide(){
    return side;
}

public void setBoxIndex(int index){
    this.boxIndex = index;
}
public int getBoxIndex(){
    return boxIndex;
}

public void flipSide(){
    if(side == ESide.BACK)
        side = ESide.FRONT;
    else
        side = ESide.BACK;
}

public Object clone(){
    Flashcard o = null;
    try {
        o = (Flashcard)super.clone();
    } catch (CloneNotSupportedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return o;
}
public boolean equals(Object o){
    if(this == o)
        return true;
    if(o == null)
        return false;
    if(!(o instanceof Flashcard))
        return false;
    Flashcard temp = (Flashcard)o;
    if(!temp.challenge.equals(this.challenge) || !temp.response.equals(this.response)){
        return false;
    }

    return true;
}

}

The serialize code I have done is like this:

import java.io.*;

public class Savingbox implements Serializable {
public static void main(String[] args){
    Box e = new Box();

So how can I make the savingbox class save the result that the user just used?

CPKNIGHT
  • 21
  • 2
  • You serialize a serializable object most easily by writing it to an `ObjectOutputStream`. When you try to do so with instances of your class, you will probably find that you need to make class `Flashcard` implement `Serializable`, too. – John Bollinger Oct 27 '16 at 18:29
  • So in fact I should make these two classes both serializable but not the only box one? So when I use serialization, it will not fact anything to the original class right? – CPKNIGHT Oct 27 '16 at 19:54
  • If you want to serialize an object, then you have to be able to serialize every object reachable from it. In your case, that is likely to include some `Flashcard` instances. There are ways to do that other than making all the classes involved serializable, but if you can do then making them all serializable is probably the easiest option. As for the `java.io.Serializable` interface itself, it has no methods, so classes that want to implement it need only declare that they do so. – John Bollinger Oct 27 '16 at 20:10
  • Thx. NowI have idea what's going to do. – CPKNIGHT Oct 27 '16 at 20:15

1 Answers1

-1

From what I understand you want to save the Flashcard objects to a file and then read those back in. Here is a link that explains how to: How to write and read java serialized objects into a file

Community
  • 1
  • 1
donlys
  • 440
  • 6
  • 13
  • Having a link to another SO question as the main info in an answer obviously qualifies this question as a duplicate, so it should be closed as exactly that. And if your main attempt was to get clarification (about what to understand), this is more a comment than an answer. – Seelenvirtuose Oct 27 '16 at 18:42
  • 1
    Yes. This is what I want. Save the flashcard object to file and read back when I get in. So my question is should I add any code to the flashcard class or box class like near !exit to make a construe so I can call it in the serialization class? – CPKNIGHT Oct 27 '16 at 19:57
  • and when I do serialization, how can I make sure the field is serializable? I mean without test it. – CPKNIGHT Oct 27 '16 at 19:58
  • You need to mark Flashcard as Serializable as well. Can you save Box itself? That way it will save everything inside box including Flashcards. Here is a code for that: public class Savingbox implements Serializable { public static void main(String[] args) { Savingbox savingbox = new Savingbox(); savingbox.saveBox(); savingbox.buildBox(); } – donlys Oct 27 '16 at 20:37
  • public void saveBox() { ObjectOutputStream oos = null; FileOutputStream fout = null; try { Box box = new Box(); fout = new FileOutputStream("c:\\dev\\deleteme\\flashcard.ser"); oos = new ObjectOutputStream(fout); oos.writeObject(box); } catch (IOException e) { e.printStackTrace(); } finally { if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } – donlys Oct 27 '16 at 20:37
  • public void buildBox() { ObjectInputStream objectinputstream = null; try { FileInputStream streamIn = new FileInputStream( "c:\\dev\\deleteme\\flashcard.ser"); objectinputstream = new ObjectInputStream(streamIn); Box box = (Box) objectinputstream.readObject(); System.out.println(box); } catch (Exception e) { e.printStackTrace(); } finally { if (objectinputstream != null) { try { objectinputstream.close(); } catch (IOException e) { e.printStackTrace(); } } } } – donlys Oct 27 '16 at 20:37
  • If you want to call these methods from inside of Box - you can make those methods static. That way they are part of the class definition and are not serialized. – donlys Oct 27 '16 at 20:46