Here is a class the does the writing, but i am getting exceptions saying
its not serialized.etc.
import java.io.*;
public class WriteStudentDat a {
/** Creates a new instance of WriteStudentDat a */
public WriteStudentDat a() {
}
// method will take the student list and save it to student.dat file.
public void save(Student[] student)
{
try {
ObjectOutputStr eam objOut = new ObjectOutputStr eam(new
FileOutputStrea m("student.dat" ));
objOut.writeObj ect(student);
objOut.close();
}
catch (Exception e) {
e.printStackTra ce();
}
}
}
DL wrote:
Hi
>
I have an array of objects:
>
Student[] studentarray;
>
how do I save studentarray to file?
>
David.
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 WriteStudentDat a {
>
>
/** Creates a new instance of WriteStudentDat a */
public WriteStudentDat a() {
}
>
// method will take the student list and save it to student.dat file.
public void save(Student[] student)
{
try {
ObjectOutputStr eam objOut = new ObjectOutputStr eam(new
FileOutputStrea m("student.dat" ));
objOut.writeObj ect(student);
objOut.close();
}
catch (Exception e) {
e.printStackTra ce();
}
>
}
}
Read the Javadocs on java.io.Seriali zable. It will explain why you cannot use
ObjectOutputStr eam 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.
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.
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 WriteStudentDat a {
>>
>>
> /** Creates a new instance of WriteStudentDat a */
> public WriteStudentDat a() {
> }
>>
> // method will take the student list and save it to student.dat file.
> public void save(Student[] student)
> {
> try {
> ObjectOutputStr eam objOut = new ObjectOutputStr eam(new
>FileOutputStre am("student.dat "));
> objOut.writeObj ect(student);
> objOut.close();
> }
> catch (Exception e) {
> e.printStackTra ce();
> }
>>
> }
>}
>
Read the Javadocs on java.io.Seriali zable. It will explain why you
cannot use ObjectOutputStr eam 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.
>
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 ?
>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' -
Comment