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



Lew wrote:
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?
.



Relevant Pages

  • Re: How to write Array of Object to a file?
    ... not even to your own posts. ... // 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: How to write Array of Object to a file?
    ... Here is a class the does the writing, but i am getting exceptions saying its not serialized.etc. ... public class WriteStudentData { ... // method will take the student list and save it to student.dat file. ... ObjectOutputStream objOut = new ObjectOutputStream(new ...
    (comp.lang.java)
  • 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: Elimnating namespace in XML
    ... public class Student ... // Create an instance of the object to serialize ... Student student = new Student; ... // Serialize the object with our own namespaces ...
    (microsoft.public.dotnet.framework)
  • Re: Serialization and inheritance
    ... serialization framework will be able to load the appropriate type/field data ... > I'm getting an error when I try to serialize a class in my project. ... > public class Student: Person ... > since I only need to access PersonData and nothing else. ...
    (microsoft.public.dotnet.languages.csharp)