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;
    }
}
7
  • 1
    See this answer: stackoverflow.com/a/6683609/597657 Commented Mar 21, 2012 at 16:17
  • @Eng.Fouad I tried that, but I think I failed. haha (i added more code above too) Commented Mar 21, 2012 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. Commented Mar 21, 2012 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? Commented Mar 21, 2012 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. Commented Mar 21, 2012 at 17:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am running it under the buttons actionlistener, I added more code in the post of what I got so far.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.