3

I have to, quote on quote,

1.Save accounts to a binary (serialized) file. 2.Load (recreate) accounts from a binary (serialized) file.

So firstly, I was looking up examples of what exactly that is and I am lost, in same scenarios people mention xml, in my head I think it means like 01010011000 (binary), and when I look at other code it looks like a normal text file save.

What exactly does he mean, and can someone post an example, or give me a site that better clarifies this? Once I see what I actually need to do, I can implement it easily, I'm just confused on what exactly is being saved (data-wise) and how.

*I already have an option to save via textfile (.txt) if I can just use some of that code for this binary part.

Thanks!

Here is what I have now, it's still not working I think.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SerializationMain implements Serializable {

    public static void saveSerialized(Object YourObject, String filePath) throws IOException {
        ObjectOutputStream outputStream = null;
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(filePath + ".dat"));
            outputStream.writeObject(YourObject);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static Object loadSerialized(String filePath, Object[][] data1) throws IOException {
        try {
            FileInputStream fileIn = new FileInputStream(filePath);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            try {
                data1 = (Object[][]) in.readObject();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SerializationMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println(data1.length);
        return data1;
    }
}
Austin
  • 3,010
  • 23
  • 62
  • 97
  • 1
    See this answer: http://stackoverflow.com/a/6683609/597657 – Eng.Fouad Mar 21 '12 at 16:17
  • @Eng.Fouad I tried that, but I think I failed. haha (i added more code above too) – Austin Mar 21 '12 at 16:45
  • Wauw! First of all, re-throw those exceptions or at least log them. You have no clue as to what has happened if you leave your catch blocks empty. That might give you a clue as to what is happening. – smox Mar 21 '12 at 17:15
  • @smox well, yea...but how my serialization save/load setup right or wrong? I just want to save the double array object, then get it back. And something isn't working. As for catch blocks they are only dealing with file exceptions right? I know the file reloads, and I know it saves, so even if I log them it won't matter at this step, correct? – Austin Mar 21 '12 at 17:19
  • You cannot save types of java.lang.Object as it does not implement the interface Serializable which is required for Object serialization. – smox Mar 21 '12 at 17:38
  • @smox Hmm ok, well I am running this all in Neatbeans, so is there an implement feature on the action listener part of that, or do I need to have to run a side method that utilizes serialize? – Austin Mar 21 '12 at 17:52
  • You are missing some fundamental points of the java language. Take your time to read the following: http://www.ecst.csuchico.edu/~amk/foo/advjava/notes/serial.html and then it should be very clear. It is the object to be saved that should be serializable and not the object/class that is saving the object. – smox Mar 21 '12 at 21:22

2 Answers2

1

First hit on google: http://www.javacoffeebreak.com/articles/serialization/index.html - basically you should serialize your object to a file. Then you can load it into an object again later.

smox
  • 315
  • 3
  • 11
  • See, I need more to go off than that, I have no clue what the save file should even look like, I also added more code in the post of what I got so far. – Austin Mar 21 '12 at 16:43
1

Assuming you have a class called "account" you simply need to implements Serializable at the top of your class header.

From my understanding, that will serialize all the data into a binary form. You will need to of course still perform the steps to actually write/read the class object out to a file using ObjectOutputStream/ObjectInputStream.

So for example...

public class account implements Serializable
{ ...
}

Then in your main function for example, where you want to save the object, you would create a File, attach it to an ObjectOutputStream and write out your object in binary form.

Matthew
  • 3,886
  • 7
  • 47
  • 84
  • I am running it under the buttons actionlistener, I added more code in the post of what I got so far. – Austin Mar 21 '12 at 16:42