• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Write a object into a file

I've got an object with 2 other objects inside it.

I want to know howto write the object to a file.
Does anybody know howto do this.

Thanx in advance
0
treshr
Asked:
treshr
1 Solution
 
CEHJCommented:
Use ObjectOutputStream. All objects must be serializable
0
 
treshrAuthor Commented:
I know that but how do you add the object to the outputstream because I couldn't find it.

0
 
TimYatesCommented:
ObjectOutputStream oos = new ObjectOutputStream( outputStream ) ;
oos.writeObject( obj ) ;
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
treshrAuthor Commented:
And outputStream is the object, isn't it.

But how does java know what file to write to.
0
 
treshrAuthor Commented:
And outputStream is the object, isn't it.

But how does java know what file to write to.
0
 
treshrAuthor Commented:
Leave it, I already fixed it, thanx.
0
 
CEHJCommented:
>>But how does java know what file to write to.

Because you will have opened the output stream on a particular file.

This should tell you all you need to know:
http://java.sun.com/docs/books/tutorial/essential/io/serializing.html
0
 
kaushikgmehtaCommented:
Append to Tim's code this

File f1=new File("*** LOCATION OF FILE ***");
FileOutoutStream fos1=new FileOutputStream(f1);

ObjectOutputStream oos=new ObjectOutputStream(fos1);

oos.writeObject(urobj);

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now