GreenAddiction
GreenAddiction

Reputation: 17

Save array in binary format

I have the following arrayList

import java.io.*;
import java.util.*;
import java.util.logging.*;

public class SaveData{
int counter =1;
public void saveTheData(ArrayList<myClass> myClassObj){
    try{
        FileOutputStream fout = new FileOutputStream(counter+"SaveGame.ser", true);
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(myClassObj.toString() );
        counter++;
        oos.close();
    } catch (Exception e) {
        e.printStackTrace();
    } 
}
}

Sorry I'm new to java so plese excuse any silly questions. The code above saves the array in ser format. I need to save it in binary format and then be able to also read it from its binary format later. I have no idea how you do this though

Any help is much appreciated

Upvotes: 0

Views: 342

Answers (2)

NanoWizard
NanoWizard

Reputation: 2164

As stated by others, if you want to write binary, don't use the toString() method when serializing the object. You also need to implement Serializable in class myClass. Then deserializing is just as simple as serializing, by using ObjectInputStream.readObject().

Your resulting SaveData class should then look something like this:

import java.io.*;
import java.util.*;

public class SaveData {
    int counter = 1;

    public void saveTheData(ArrayList<myClass> myClassObj) {
        try {
            FileOutputStream fout = new FileOutputStream(counter
                    + "SaveGame.ser", true);
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(myClassObj);
            counter++;
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ArrayList<myClass> loadTheData(int saveNum) {
        try {
            FileInputStream fin = new FileInputStream(saveNum + "SaveGame.ser");
            ObjectInputStream ois = new ObjectInputStream(fin);
            ArrayList<myClass> myClassObj = (ArrayList<myClass>) ois.readObject();
            ois.close();
            return myClassObj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

And myClass would look something like this:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class myClass implements Serializable {
    private static final long serialVersionUID = /* some UID */;
    /* ...
     * class properties
     */

    myClass(/* args */) {
        // Initialize
    }

    /* ...
     * class methods
     */

    private void writeObject(ObjectOutputStream o) throws IOException {
        // Write out to the stream
    }

    private void readObject(ObjectInputStream o) throws IOException,
            ClassNotFoundException {
        // Read in and validate from the stream
    }
}

Upvotes: 1

Hans Lepoeter
Hans Lepoeter

Reputation: 149

Well .. myClassObj.toString()

Generates a String and thats what you are writing to the output stream. You can expect a string and some other data defining it's size in that case and not a binary representation.

You could just use :

oos.writeObject(myClassObj );

That's because ArrayList implements the Serializable interface. However you will depend on that implementation for what is actually written in that case. The only contract the Serializable interface must support is that the object can be recreated as it was with just the data written to the file.

If you want to write data to the file in raw binary format ( perhaps to read it later from c code for example ) you must write something yourself which cycles the ArrayList. That code would depend on the implementation of myClass so it's hard to give a working examples.

Something like :

for (myClass temp : myClassObj) 
{
        temp.writeBinaryDataToStream(oos);
}

Where writeBinaryDataToStream(oos) is up to you to implement.

Upvotes: 0

Related Questions