OBJECTSTREAM CLASS
import [Link];
import [Link];
/**
* [Link]
* This class represents a Student
*
* @author [Link]
*/
public class Student implements Serializable
{
private String name;
private Date birthday;
private boolean gender; // true is male, false is female
private int age;
private float grade;
public Student(String name, Date birthday,boolean gender, int age, float grade)
{
[Link] = name;
[Link] = birthday;
[Link] = gender;
[Link] = age;
[Link] = grade;
}
public String getName()
{
return [Link];
}
public void setName(String name)
{
[Link] = name;
}
public Date getBirthday()
{
return [Link];
}
public void setBirthday(Date birthday)
{
[Link] = birthday;
}
public boolean getGender()
{
return [Link];
}
public void setGender(boolean gender)
{
[Link] = gender;
}
public int getAge()
{
return [Link];
}
public void setAge(int age)
{
[Link] = age;
}
public float getGrade()
{
return [Link];
}
public void setGrade(float grade)
{
[Link] = grade;
}
}
As you can see, this class implements the Serializable interface and has 5 member
variables of various data types. We will write two programs for writing and reading objects of this
class using the ObjectInputStream and ObjectOutputStream classes.
1 import [Link].*;
2 import [Link].*;
3 import [Link].*;
4
5 /**
* [Link]
6 * This program illustrates how to use the ObjectOutputStream class for writing
7 * a list of objects to a file.
8 *
9 * @author [Link]
10 */
11 public class StudentRecordWriter
{
12
13
public static void main(String[] args)
14 {
15 if ([Link] < 1)
16 {
17 [Link]("Please provide output file");
18 [Link](0);
19 }
20
21 String outputFile = args[0];
22 DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
23
try
24 {
25 ObjectOutputStream objectOutput= new ObjectOutputStream(new
26 FileOutputStream(outputFile);
27
ArrayList<Student> listStudent = new ArrayList<>();
28
[Link](new Student("Alice", [Link]("02-15-
29 1993"), false, 23, 80.5f));
30
31 [Link](new Student("Brian", [Link]("10-03-
32 1994"), true, 22, 95.0f));
33 [Link](new Student("Carol", [Link]("08-22-
34 1995"), false, 21, 79.8f));
35
36 for (Student student : listStudent)
37 {
38 [Link](student);
}
39
40 }
41 catch (IOException | ParseException ex)
42 {
43 [Link]();
44 }
45 }
}
46
47
48
Writing Objects to a File by using the ObjectOutputStream
class:
Run this program via command line:
java StudentRecordWriter [Link]
You will see a file named [Link] created, but it is in binary format, which means you
cannot view its content using a text editor.
Reading Objects from a File by using the
ObjectInputStream class:
1 import [Link].*;
2 import [Link].*;
3
4 /**
* [Link]
5 * This program illustrates how to use the ObjectInputStream class for reading
6 * objects from a file.
7 *
8 * @author [Link]
9 */
10 public class StudentRecordReader
{
11
12
13 public static void main(String[] args)
14 {
15 if ([Link] < 1)
{
16 [Link]("Please provide input file");
17 [Link](0);
18 }
19
20 String inputFile = args[0];
21 DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
22
23 try
24 {
ObjectInputStream objectInput
25
= new ObjectInputStream(new FileInputStream(inputFile);
26
while (true)
27 {
28 Student student = (Student)
29 [Link]();
30 [Link]([Link]() + "\t");
31 [Link]([Link]([Link]()) + "\t");
32 [Link]([Link]() + "\t");
[Link]([Link]() + "\t");
33 [Link]([Link]());
34 }
35
36 }
37 catch (EOFException eof)
38 {
[Link]("Reached end of file");
39 }
40 catch (IOException | ClassNotFoundException ex)
41 {
42 [Link]();
43 }
44 }
}
Output:
Run this program via command line:
java StudentRecordReader [Link]
1 Alice 02-15-1993 false 23 80.5
2 Brian 10-03-1994 true 22 95.0
3 Carol 08-22-1995 false 21 79.8
Reached end of file
4