Java Reference
In-Depth Information
Now, you'll create a simple example that writes an object to a file and reads the same object
back in. The created object will contain a vector of other objects. Listing 6.1 contains the
object you will be serializing.
import java.util.*;
public class StudentList implements Serializable {
// Vector holding our students
Vector list = new Vector(6);
// Default Constructor
public StudentList() {
public void addStudent(String value) {
// Add a String representing a student name
if ( value != null ) {
public void listStudents() {
// Iterate over list vector, printint all Strings
for ( int x = 0; x < list.size(); x++ ) {
System.out.println(“Student “ + x + “ : “
+ (String)list.elementAt(x));
Notice that the StudentList object implements the interface. It also
contains a vector of strings representing student names, which are also defined as
Serializable .
The StudentListApplication will be used to create, store, and retrieve the StudentList from
persistent storage. In this example, the persistent storage is the file file.dat . The
StudentListApplication is found in Listing 6.2.
Search WWH ::

Custom Search