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.
ObjectInputStream from the end of a file
Page 1 of 13 Replies - 209 Views - Last Post: 03 October 2012 - 09:39 AM
Replies To: ObjectInputStream from the end of a file
#2
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
#3
Re: ObjectInputStream from the end of a file
Posted 03 October 2012 - 09:23 AM
g00se, 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()
#4
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|