7

I've seen so many different posts about what way you're supposed to serialize an object to a file, and all of them conflict in nature on how to do it and what the best practices are. So here's what I'm trying to save:

public class IHandler{
    public double currentLoad;
    public String currentPrice;
    public String configArgs[];
};

We can assume that the size of configArgs is known that I need to make a file, here's what I have so far.

public static void serializeDataOut(IHandler ISH)throws IOException{
    String fileName= "Test.txt";
    FileOutputStream fos = new FileOutputStream(fileName);
        //What do I do here?
    }

  public static IHandler serializeDataIn(){
      //What do I do here?
  }
A_Elric
  • 3,508
  • 13
  • 52
  • 85
  • 1
    You have to decide a format first. Binary? XML? JSON? Proprietary? – JB Nizet May 18 '12 at 14:18
  • 1
    *"and all of them conflict in nature on how to do it and what the best practices are."* The best way often depends on the exact requirements. Object serialization can be good for particular situations, but the lack of guarantee of readability in future JREs puts limits on its utility. To get the best answers, provide more context. Are you saving 1 object or 100,000? Is it at start-up/shut-down that the read/write happens, by choice of the user, or automatically every 87 milliseconds? – Andrew Thompson May 18 '12 at 14:32
  • I wouldn't mind it being in xml, but if that's not possible, well, I could write it in something else and just pull the whole object in later on. – A_Elric May 18 '12 at 14:54
  • 2
    I see no question here. – Raedwald Aug 09 '13 at 12:27

4 Answers4

11

Well I assume, you want to write object directly into the file

public static void serializeDataOut(IHandler ish)throws IOException{
    String fileName= "Test.txt";
    FileOutputStream fos = new FileOutputStream(fileName);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(ish);
    oos.close();
}

public static IHandler serializeDataIn(){
   String fileName= "Test.txt";
   FileInputStream fin = new FileInputStream(fileName);
   ObjectInputStream ois = new ObjectInputStream(fin);
   IHandler iHandler= (IHandler) ois.readObject();
   ois.close();
   return iHandler;
}

I just provided important code. Implement this with exception handling.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
4

An example of how to serialize an object:

public static void writeToFile(File path, Database data)
{
    try(ObjectOutputStream write= new ObjectOutputStream (new FileOutputStream(path)))
    {
        write.writeObject(data);
    }
    catch(NotSerializableException nse)
    {
        //do something
    }
    catch(IOException eio)
    {
        //do something
    }
}


public static Object readFromFile(File path)
{
    Object data = null;

    try(ObjectInputStream inFile = new ObjectInputStream(new FileInputStream(path)))
    {
        data = inFile.readObject();
        return data;
    }
    catch(ClassNotFoundException cnfe)
    {
        //do something
    }
    catch(FileNotFoundException fnfe)
    {
        //do something
    }
    catch(IOException e)
    {
        //do something
    }
    return data;
}   

For more info http://docs.oracle.com/javase/tutorial/jndi/objects/serial.html

Martin
  • 1,130
  • 10
  • 14
1

You can use the XMLDecoder/XMLEncoder to serialize JavaBean as xml. Here are the examples from oracle's Javadocs on the two classes:

(XMLDecoder)

   XMLDecoder d = new XMLDecoder(
                      new BufferedInputStream(
                          new FileInputStream("Test.xml")));
   Object result = d.readObject();
   d.close();

(XMLEncoder)

   XMLEncoder e = new XMLEncoder(
                      new BufferedOutputStream(
                          new FileOutputStream("Test.xml")));
   e.writeObject(new JButton("Hello, world"));
   e.close();

Note that you would need to add getters and setters and make the class serializable.

Sanjeev
  • 1,517
  • 1
  • 18
  • 30
0

do you want to write IHandler into test.txt file? try

ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.write(IHandler);

if you want read IHandler from that file, need do this

ObjectInputStream ois = new ObjectInputStream(fos); 
IHandler iHandler = (IHandler) ois.readObject();
user1335794
  • 1,082
  • 6
  • 12