The Artima Developer Community
Sponsored Link

Java Answers Forum
saving objects on a file

3 replies on 1 page. Most recent reply: Apr 1, 2002 8:04 AM by Jay Kandy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

saving objects on a file Posted: Mar 29, 2002 12:29 PM
Reply to this message Reply
Advertisement
how can i write/read objects on file?is there file of
objects?
is there any way to write objects on file other than
serilization ?

thanks


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: saving objects on a file Posted: Mar 29, 2002 1:07 PM
Reply to this message Reply
> how can i write/read objects on file?is there file
> of
> objects?

There is
java.io.ObjectInputStream
and
java.io.ObjectInputStream
to read/write objects.

> is there any way to write objects on file other than
>
> serilization ?

I dont understand "other than serialization". Serializing an object means "saving the state of an object independant of the JVM"

Jay

Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Re: saving objects on a file Posted: Mar 31, 2002 3:52 PM
Reply to this message Reply
ok thanks ,i find it..

but when i use this file in my program ,each time i run
the program the file will be cleared ,not as RandoAccessFile in which the contents of it still in the file..

i need to store list of objects in file and keep them
in this file and when running the program next time
not to cleare these objects

how can i do this?
is there in ObjectStream read+write as RandomAccess..

thanks alot

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: saving objects on a file Posted: Apr 1, 2002 8:04 AM
Reply to this message Reply
Hmmm.. Let me see if I can explain properly. (To avoid complexity) there are two pairs of abstract classes for performing system input and output through streams, file system and serialization. All classes that provide i/o (except RandomAccessFile) are derived from these abstract classes. They are java.io.InputStream & java.io.OutputStream and java.io.Reader

One simple rule of thumb to remember is:
All Writers & Readers read from/write to character streams.
All InputStreams & OutputStreams read from/write to byte streams.

Now thats out of the way, guess which classes you would use to do i/o on a File system? Obviously, the pairs:FileWriter & FileReader and FileInputStream & FileOutputStream. Of course you would want to use wrappers around them (BufferedOutputStream).

Now to answer your question, the classes FileOutputStream and FileWriter provide constructors that take a boolean flagging the class to append to the file or not. So you might need something along (using ObjectOutputStream javadoc example):
FileOutputStream ostream = new FileOutputStream("t.tmp", true);
ObjectOutputStream p = new ObjectOutputStream(ostream);

Hope this answered your question.

Sincerely,
Jay

Flat View: This topic has 3 replies on 1 page
Topic: Banner which displays Three Images Previous Topic   Next Topic Topic: i need practical info of the GRI & the GRI_RRP implementation plz

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use