开发者

How do we save(serialize) an object of a GUI Class to a file in Java?

I have a GUI class in netbeans that retrieves Tweets from the twitter search API ,it contains TextAreas, Labels and Editor panes that shows the source user, time of posting, tweet text etc... I w开发者_如何转开发ant the tweets ( to be saved in a file and I need to serialize the class, so I can save the whole tweet as an object in a file. Any help?


Don't store your whole GUI object as there will be plenty of stuff associated with the controls that you don't need. Instead create a custom serializer, sounds impressive but is actually very simple.

take this simple example, the class has a frame that has the name of the user up top, this is not serialized as the detail to create it is simply the name so don't need to store the rest of the JFrame object. The other details are simple properties that can be serialised.

public class Simple implements Serializable {

    private transient JFrame win;
    private String username;
    private Date postTime;
    //...assume getters/setters constructors e.t.c.
    private void writeObject( ObjectOutputStream out ) throws IOException {
        out.write( this );
        out.flush();
    }
    private Object readObject( ObjectInputStream in ) throws IOException, ClassNotFoundException {
        Simple s = (Simple) in.read();
        s.setWin( new JFrame( s.getUsername() ) );
        //...any other extra setup can be done here
        return s;
    }

}

As you can see I've tried to keep things simple, note I have just written this so haven't tried it yet it's more of a "starter for 10" than a complete solution. You can customize the serialisation for example you could send a Long to the stream instead of the complete Date object i.e. the output could have been out.write( username ); out.write( postTime.getTime() ); we are of course allowing autoboxing to take care of turning our long into a Long, so there is a possibility that some sneaky jdk might box it to an Integer instead and lose some precision :) plus changes to the order objects are written to the stream should be mirrored with the order they are read from the stream.

Anyway once this is done you can write the objects to a file like so

ObjectOutputStream oout = new ObjectOutputStream( new FileOutputStream( "blah.bin" ) ); Simple s = new Simple(); //...set some values on s oout.writeObject( s ); oout.flush(); oout.close(); ObjectInputStream oin = new ObjectInputStream( new FileInputStream( "blah.bin" ) ); Simple simple = (Simple) oin.readObject(); oin.close();

obviously I've not included the mass of extra syntax needed for exception handling and efficient stream usage but it's just to give a few ideas.

If this doesn't take your fancy then you could always try using the object graph to xml serializer, that's quite handy. "java.beans.XMLEncoder" this was a very handy class when storing simple beans in text fields in a db :)


This sounds like something you should most definitely not do. You should instead just save the tweets in a file. Serializing should be your LAST resort


As other have suggested, serialize data, not GUI objects.

Now: data itself comes most likely as JSON, so you should be able to just write that JSON out, using same tool as you use for parsing it (hopefully something like Jackson).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜