Write a object into a file

Posted on 2003-03-05
Medium Priority
Last Modified: 2010-03-31
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
Question by:treshr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 8070823
Use ObjectOutputStream. All objects must be serializable

Author Comment

ID: 8070830
I know that but how do you add the object to the outputstream because I couldn't find it.

LVL 35

Accepted Solution

TimYates earned 90 total points
ID: 8070877
ObjectOutputStream oos = new ObjectOutputStream( outputStream ) ;
oos.writeObject( obj ) ;
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more


Author Comment

ID: 8071012
And outputStream is the object, isn't it.

But how does java know what file to write to.

Author Comment

ID: 8071038
And outputStream is the object, isn't it.

But how does java know what file to write to.

Author Comment

ID: 8071043
Leave it, I already fixed it, thanx.
LVL 86

Expert Comment

ID: 8071049
>>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:

Expert Comment

ID: 8071069
Append to Tim's code this

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

ObjectOutputStream oos=new ObjectOutputStream(fos1);



Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question