AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to write Array of Object to a file?

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java
View previous topic :: View next topic  
Author Message
DL
Guest





PostPosted: Mon May 07, 2007 4:54 pm    Post subject: How to write Array of Object to a file? Reply with quote



Hi

I have an array of objects:

Student[] studentarray;

how do I save studentarray to file?

David.
Back to top
DL
Guest





PostPosted: Mon May 07, 2007 5:00 pm    Post subject: Re: How to write Array of Object to a file? Reply with quote



Here is a class the does the writing, but i am getting exceptions saying
its not serialized.etc.

import java.io.*;

public class WriteStudentData {


/** Creates a new instance of WriteStudentData */
public WriteStudentData() {
}

// method will take the student list and save it to student.dat file.
public void save(Student[] student)
{
try {
ObjectOutputStream objOut = new ObjectOutputStream(new
FileOutputStream("student.dat"));
objOut.writeObject(student);
objOut.close();
}
catch (Exception e) {
e.printStackTrace();
}

}
}

DL wrote:
Quote:
Hi

I have an array of objects:

Student[] studentarray;

how do I save studentarray to file?

David.
Back to top
Lew
Guest





PostPosted: Mon May 07, 2007 5:34 pm    Post subject: Re: How to write Array of Object to a file? Reply with quote



Please do not top-post, not even to your own posts.

DL wrote:
Quote:
Here is a class the does the writing, but i am getting exceptions saying
its not serialized.etc.

I doubt very much that any error message included the word "etc." Are you
certain that you copied and pasted the error messages correctly?

Quote:
import java.io.*;

public class WriteStudentData {


/** Creates a new instance of WriteStudentData */
public WriteStudentData() {
}

// method will take the student list and save it to student.dat file.
public void save(Student[] student)
{
try {
ObjectOutputStream objOut = new ObjectOutputStream(new
FileOutputStream("student.dat"));
objOut.writeObject(student);
objOut.close();
}
catch (Exception e) {
e.printStackTrace();
}

}
}

Read the Javadocs on java.io.Serializable. It will explain why you cannot use
ObjectOutputStream yet for the Student type.

Incidentally you get better results on newsgroups if you post an SSCCE.
Without the source for the Student class in hand we have to guess (correctly)
why you cannot serialize the class.

BTW, there are other ways to write information to a file than serialization.

--
Lew
Back to top
Robert Larsen
Guest





PostPosted: Mon May 07, 2007 5:47 pm    Post subject: Re: How to write Array of Object to a file? Reply with quote

Lew wrote:
Quote:
BTW, there are other ways to write information to a file than
serialization.


Absolutely. In fact you should be very carefull when using serialization.
For example if each student has a reference to his class (school class
that is) and the school class object has references to the students that
attend that class you may save the entire class when you want to save
one student. Do that for each student in the class and you have a huge
overhead.
The other ways are often better...XML, SQL database, flat text files,
binary files. There are lots to choose from.
Back to top
DL
Guest





PostPosted: Mon May 07, 2007 5:50 pm    Post subject: Re: How to write Array of Object to a file? Reply with quote

Lew wrote:
Quote:
Please do not top-post, not even to your own posts.

DL wrote:
Here is a class the does the writing, but i am getting exceptions
saying its not serialized.etc.

I doubt very much that any error message included the word "etc." Are
you certain that you copied and pasted the error messages correctly?

import java.io.*;

public class WriteStudentData {


/** Creates a new instance of WriteStudentData */
public WriteStudentData() {
}

// method will take the student list and save it to student.dat file.
public void save(Student[] student)
{
try {
ObjectOutputStream objOut = new ObjectOutputStream(new
FileOutputStream("student.dat"));
objOut.writeObject(student);
objOut.close();
}
catch (Exception e) {
e.printStackTrace();
}

}
}

Read the Javadocs on java.io.Serializable. It will explain why you
cannot use ObjectOutputStream yet for the Student type.

Incidentally you get better results on newsgroups if you post an SSCCE.
Without the source for the Student class in hand we have to guess
(correctly) why you cannot serialize the class.

BTW, there are other ways to write information to a file than
serialization.


what is the best way to do this? any suggestions?
Back to top
Robert Larsen
Guest





PostPosted: Mon May 07, 2007 9:34 pm    Post subject: Re: How to write Array of Object to a file? Reply with quote

DL wrote:
Quote:
what is the best way to do this? any suggestions?

That depends on a lot of things. An XML file i very easily read by
humans and portable but often not very scalable. A database is very fast
and scalable. A binary file can be easy to work with (or very very hard).
What would be the typical usage ? And how many records would the file hold ?
Back to top
Lew
Guest





PostPosted: Tue May 08, 2007 5:10 pm    Post subject: Re: How to write Array of Object to a file? Reply with quote

Robert Larsen wrote:
Quote:
DL wrote:
what is the best way to do this? any suggestions?

That depends on a lot of things. An XML file i very easily read by
humans and portable but often not very scalable. A database is very fast
and scalable. A binary file can be easy to work with (or very very hard).
What would be the typical usage ? And how many records would the file hold ?

There are gazillion ways to emit data. The simplest is to open a file for
writing, say in a variable 'wr' -

import java.io.PrintWriter;
...
PrintWriter wr = new PrintWriter( someFileName );
...
wr.println( foo.getProperty().toString() );

Of course, 'toString()' in most cases will not give you what you want, but
it's a start.

Really you should just read the Javadocs on the java.io package, and the tutorial:
<http://java.sun.com/docs/books/tutorial/essential/io/index.html>

--
Lew
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.