Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Reading/writing object & other data to a file

Thread view: 
Erin K - 07 Aug 2006 12:19 GMT
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.
Chris Uppal - 07 Aug 2006 13:13 GMT
> 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...
William Brogden - 07 Aug 2006 15:07 GMT
> 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
[quoted text clipped - 7 lines]
>     fos.write(next byte);
> }

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

> Reading:
> FileInputStream fis = new FileInputStream(myfile);
[quoted text clipped - 6 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.