Java Help

Discussion in 'Software' started by Adrynalyne, Sep 18, 2012.

  1. Adrynalyne

    Adrynalyne Guest

    I'll throw this out there, and maybe get a bite.

    I am writing a program that takes a contact and stores it as an object in a binary file. I do this for each time someone clicks the 'save' button. I then have a viewer that reads the contacts back every time someone clicks the next button. The program needs to be able to read back at any time, even after the program has been closed and re-opened.

    My questions:

    How do I read back all objects? Once I get to the 2nd+ objects, I get a streamcorrupted exception.

    How do I code the ActionListener for the button to read, pause loop to return data, and then read loop again on next button press? I think I can probably do this via Array, but then I have my streamcorrupted exception again.
     
  2. PC-XT

    PC-XT Master Sergeant

    It sounds like you are trying to find a way to serialize objects in such a way to allow appending without ObjectOutputStream corrupting the stream by writing a new header each time it's created. I usually don't serialize objects to do this, but either customize my own fixed-size data fields, or variable-length blobs, instead, making the file more like a database. If you want to serialize objects, though, you may be able to do this:

    Method A: The method the designers had in mind seems to be loading/saving the entire file at start/end of program or change of data. For the easiest management, put all objects in a container, such as the Array you mentioned. Overwrite the data file by sending the container to the ObjectOutputStream. When the program loads, you then just read the container from the ObjectInputStream, modify at will, and resave on exit (or when there is a change, to avoid data loss on crashing.) This could be slow if you have many large objects. You may not need a container, if you just run them all through the same ObjectOutputStream, but it tends to be easier to manage as one object.


    Other methods somewhat exist, but I wouldn't recommend them, at least not yet:

    Method B: Try to make an appendable ObjectStream. This is complicated, and some implementations can be nearly as bad as Method C: http://stackoverflow.com/questions/...pending-object-outputstream/12438141#12438141 I wish I had a good one to recommend. It would basically need to read the full stream, and pretend it just rewrote the same stream to go from there.

    Method C: Appending new ObjectOutputStreams anyway. When reading, correct for it by splitting the data into different ObjectOutputStreams for separate reading. I don't like this method, myself, except as a last resort. It is very inefficient, because it often ends up needlessly repeating not only the header, but objects, as well. Java's string handling is wasted, as well as disk space. Even though redundancy can help prevent data loss, it would be hard to get that advantage without taking extra precautions to split the streams correctly if some are corrupted. I mention this method only hoping you will avoid it.

    See also:
    http://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream
     
    Last edited: Sep 18, 2012
  3. PC-XT

    PC-XT Master Sergeant

    Sorry to double post. I didn't mention this since you said you could use an Array, but in case someone finds this useful for large files that won't fit in memory or something, here is another method that uses 2 files:
    When the program starts, it should rename the data file, if it exists. It creates a new data file with an ObjectOutputStream, reads the objects from the old data file's ObjectInputStream, sending them directly to the ObjectOutputStream. New objects can be appended by sending them to that same ObjectOutputStream. When the program ends, delete the old data file.
     
  4. Adrynalyne

    Adrynalyne Guest

    I'll take a look at your suggestions.

    Thanks!
     
  5. PC-XT

    PC-XT Master Sergeant


MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds