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: 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)
  • Re: machiaveliin narcissists
    ... psychologists or the reputation of the psychology profession---only ... kept saying all the nasty posts posted via Altopia were AC's ... months owing to her CRAZY fanstasies ---I gave a rat's ass what a ... crazy student posted... ...
    (sci.psychology.psychotherapy)
  • Re: Serialization and inheritance
    ... That's a pity. ... Forces me to move my student class to the shared assembly, ... >> I'm getting an error when I try to serialize a class in my project. ... >> public int PersonData; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Who Are You??
    ... Norman Draper wrote: ... Reading the posts brought to mind things I should have included in my ... I fell in love with guitars around 1965 and began playing and teaching myself, I was a bad guitar teacher and student, I played very badly for the next 40 years. ...
    (rec.music.makers.guitar.acoustic)