Sagewire Logo

How to write Array of Object to a file?

7 Message(s) by 3 Author(s) originally posted in java programming


From: DL Date:   Monday, May 07, 2007
Hi

I have an array of object s:

Student[] studentarray;

how do I save studentarray to file ?

David.


From: DL Date:   Monday, May 07, 2007
Here is a class the does the writing, but I am getting exceptions saying
its not serialize d.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.write Object(student);
objOut.close();
}
catch (Exception e) {
e.printStackTrace();
}

}
}

wrote in message :
Hi
I have an array of objects:
Student[] studentarray;
how do I save studentarray to file?
David.






From: Lew Date:   Monday, May 07, 2007
Please don't top-post, not even to your own posts.

wrote in message:
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'll 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've to guess (correctly)
why you cannot serialize the class.

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

--
Lew


From: Robert Larsen Date:   Monday, May 07, 2007
wrote in message:
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've a huge
overhead.
The other ways are often better...XML , SQL database , flat text files,
binary files. There are lots to choose from.


From: DL Date:   Monday, May 07, 2007
wrote in message:
Please don't top-post, not even to your own posts.
wrote in message:
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'll 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've 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?


From: Robert Larsen Date:   Monday, May 07, 2007
wrote in message:
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'd be the typical usage ? And how many records'd the file hold ?


From: Lew Date:   Tuesday, May 08, 2007
wrote in message:
wrote in message:
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'd be the typical usage ? And how many records'd 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 won't 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



Next Message: JAVA Web Start


Blogs related to How to write Array of Object to a file?

Object doesn't support this property or method
I did not write this. It worked fine on the older server, but we can't get it to run. I'm not very proficient in Java and any help would be GREATLY appreciated. Sincerly, Joseph Russell.

Putting files in Jlist
files = new ArrayList(); User user = new User(); } //Write files to array list object public void writeFiles() { user.getName(); try{ File fileo = new File( "H:/SDI/MileStoneMyWorkSpace/MileStoneMyWorkSpace/users/" + user.getName() + ...

svn commit: r528088 [1/2] - in /maven/plugins/branches/MWAR-97 ...
@throws java.io.FileNotFoundException if destination is a directory - *

- * TO DO: Remove this method when Maven moves to plexus-utils version 1.4 + * @param destination A non-directory File to write bytes ...

java faqs
setHeader("Set-Cookie", "cookie string"); To give the response-object to a bean, write a method setResponse (HttpServletResponse response) - to the bean, and in jsp-file: <% bean.setResponse (response); %> 114) How can I delete a cookie ...

Java Drill - 1
37.What type of parameter passing does Java support? 38.Primitive data types are passed by reference or pass by value? 39.Objects are passed by value or by reference? 40.What is serialization? 41.How do I serialize an object to a file? ...

C# from a Java Developer's Perspective v2.0
Nested classes; Threads and Volatile Members; Operator Overloading; switch Statement; Assemblies; Collections; goto (no longer considered harmful); Virtual Methods (and final ones too); File I/O; Object Serialization ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional