3 Replies - 637 Views - Last Post: 03 October 2012 - 09:39 AM Rate Topic: -----

#1 wrightm96  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 06-March 12

ObjectInputStream from the end of a file

Posted 01 October 2012 - 08:48 AM

Hello -

I have a bizarre situation that I'm trying to code:

I have a file that already has some data in it, and I use ObjectOutputStream with a FileOutputStream set to append to add a serialized object onto that file.

Is there a way I can read that object back? I tried ObjectInputStream, and that didn't work because the file contains things other than objects when I use the readObject() method.

Thanks for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: ObjectInputStream from the end of a file

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: ObjectInputStream from the end of a file

Posted 01 October 2012 - 10:08 AM

No, you can't really use Object streams like that. If you tell us more about your use case, we might find an alternative
Was This Post Helpful? 0
  • +
  • -

#3 wrightm96  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 06-March 12

Re: ObjectInputStream from the end of a file

Posted 03 October 2012 - 09:23 AM

View Postg00se, on 01 October 2012 - 10:08 AM, said:

No, you can't really use Object streams like that. If you tell us more about your use case, we might find an alternative


What I'm doing is creating a file, writing a photo to it using ImageIO, then appending the object.

File file = new File("example.txt");
BufferedImage image = new BufferedImage(....);
ImageIO.write(image, "png", file);

FileOutputStream fos = new FileOutputStream(file, true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.write(myObject);
oos.close()


Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: ObjectInputStream from the end of a file

Posted 03 October 2012 - 09:39 AM

You've got the worst of both worlds there: you've a perfectly good png file (not a .txt file btw) that's been corrupted by writing an object to the end of it, and an object file that's corrupt owing to its being preceded by an image

This post has been edited by g00se: 03 October 2012 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1