Re: How to write Array of Object to a file?



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.

--
Lew
.



Relevant Pages

  • Re: How to write Array of Object to a file?
    ... public class WriteStudentData { ... // method will take the student list and save it to student.dat file. ... ObjectOutputStream objOut = new ObjectOutputStream(new ... Without the source for the Student class in hand we have to guess why you cannot serialize the class. ...
    (comp.lang.java)
  • Re: Pullum onThe Elements
    ... Several AUEers teach or have taught English to foreigners. ... every student* to "start with the title".... ... I've often wondered how many AUEers just click on your posts and then ... one-liners to help the reader understand your puzzling comments. ...
    (alt.usage.english)
  • Insight - Hindsight FOR ALL
    ... posts to 2 per day, so as to not dominate the board. ... But why a philosophy group; because in a philosophy group there is a ... A SETH student, a BIBLE student, a TAO student, a LAW student, ... ... just natural parts of liviing and surviving, are addictions, and we even ...
    (talk.philosophy.misc)
  • Re: The Guitar Teacher Project
    ... reading all of the posts, but I wanted to chime in. ... intuitively to the individual student. ... curriculum) and more than a good test. ... about evaluating the effectiveness of a jazz methodology. ...
    (rec.music.makers.guitar.jazz)
  • Re: Attention All Student Pilots Please; About Mxsmanic
    ... I am an instructor who posts ... here on the student forum from time to time. ... pay attention to this or that when it's posted here on the forum. ... instructors and pilots devote a great deal of our time to helping you ...
    (rec.aviation.student)