Connecting Tech Pros Worldwide Help | Site Map

How to write Array of Object to a file?

  #1  
Old May 7th, 2007, 12:55 PM
DL
Guest
 
Posts: n/a
Hi

I have an array of objects:

Student[] studentarray;

how do I save studentarray to file?

David.
  #2  
Old May 7th, 2007, 01:05 PM
DL
Guest
 
Posts: n/a

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.

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();
}

}
}

DL wrote:
Quote:
Hi
>
I have an array of objects:
>
Student[] studentarray;
>
how do I save studentarray to file?
>
David.
  #3  
Old May 7th, 2007, 01:35 PM
Lew
Guest
 
Posts: n/a

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


Please do not top-post, not even to your own posts.

DL wrote:
Quote:
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?
Quote:
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
  #4  
Old May 7th, 2007, 01:55 PM
Robert Larsen
Guest
 
Posts: n/a

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


Lew wrote:
Quote:
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.
  #5  
Old May 7th, 2007, 01:55 PM
DL
Guest
 
Posts: n/a

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


Lew wrote:
Quote:
Please do not top-post, not even to your own posts.
>
DL wrote:
Quote:
>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?
>
Quote:
>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?
  #6  
Old May 7th, 2007, 05:35 PM
Robert Larsen
Guest
 
Posts: n/a

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


DL wrote:
Quote:
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 ?
  #7  
Old May 8th, 2007, 01:15 PM
Lew
Guest
 
Posts: n/a

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


Robert Larsen wrote:
Quote:
DL wrote:
Quote:
>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' -

import java.io.PrintWriter;
...
PrintWriter wr = new PrintWriter( someFileName );
...
wr.println( foo.getProperty().toString() );

Of course, 'toString()' in most cases will not 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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing JPEG file from pixel array of ints cagdasal answers 4 February 22nd, 2008 11:02 PM
Problem to write a good serie of bytes in a file. philip answers 5 February 5th, 2006 03:15 PM
Array of Pointers to Objects Kareem Nutt answers 9 July 22nd, 2005 08:53 PM
Passing an array of chars to a function jr answers 58 July 22nd, 2005 06:36 AM