Reading/writing object & other data to a file

E

Erin K

How does one read/write a file with a serialized object, then some
other data in it, so that after one reads the object, the next byte
read is whatever came after the object in the file? I want to do
something like
Writing:
FileOutputStream fos = new FileOutputStream(myfile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(some object);
for(a bunch of bytes) {
fos.write(next byte);
}

Reading:
FileInputStream fis = new FileInputStream(myfile);
ObjectInputStream ois = new ObjectInputStream(fis);
ois.readObject();
while(fis isn't empty) {
fis.readByte();
}

but I am pretty sure that the first call to readByte will go from the
beginning of the file, not after the object like I want it to.
 
C

Chris Uppal

Erin said:
FileInputStream fis = new FileInputStream(myfile);
ObjectInputStream ois = new ObjectInputStream(fis);
ois.readObject();
while(fis isn't empty) {
fis.readByte();
}

Don't read() from fis, but from ios -- ObjectInputStream supports the usual
collection of read() methods inherited from InputStream.

-- chris

P.S. Today seems to be IO day ! This is my fifth or sixth posting on the
subject already, and it's only lunchtime...
 
W

William Brogden

How does one read/write a file with a serialized object, then some
other data in it, so that after one reads the object, the next byte
read is whatever came after the object in the file? I want to do
something like
Writing:
FileOutputStream fos = new FileOutputStream(myfile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(some object);
for(a bunch of bytes) {
fos.write(next byte);
}

Just write the byte[] object - fast, simple, and preserves the size of the
array
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top