Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving to binary/serialization java

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;
    }
}
like image 337
Austin Avatar asked Aug 06 '26 19:08

Austin


2 Answers

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.

like image 141
Matthew Avatar answered Aug 08 '26 10:08

Matthew


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.

like image 25
smox Avatar answered Aug 08 '26 08:08

smox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!