Vous êtes sur la page 1sur 4

Follow 45

Introduction
ObjectInputStream can be used to de-serialize and read
java objects that have been serialized and written using
ObjectOutputStream. In the example below the 'Course'
object is written to a persistent storage. The Course object
contains a list of Student objects. The object writing
preserves object references. We first create an object
output stream that stores the bytes to a file using a
FileOutputStream.
53 people like this. Be the
first of your friends.
Like Like Share Share
New tutorials
Java 8 - Streams (http://www.studytrails.com/java/java8/java8_streams.jsp)
Java 8 - Lambdas, Composition (http://www.studytrails.com/java/java8
/Java8_Lambdas_FunctionalProgramming.jsp)
Java 8 - Default Methods, inheritance (http://www.studytrails.com/java/java8/java8_default_methods.jsp)
(http://www.studytrails.com/java-io/character-file-reading-writing.jsp)Previous
Java IO - Read and Write Java Objects
Next (http://www.studytrails.com/java-io/randomAccessFile.jsp)
Java IO - RandomAccessFiles (http://www.studytrails.com/java-io/randomAccessFile.jsp)
Java NIO - Channels and Memory mapping (http://www.studytrails.com/java-io/Channels.jsp)
Java IO - Pipes And Print Writer (http://www.studytrails.com/java-io/pipes-print-writer.jsp)
The Course Class
Create an object output stream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
new FileOutputStream("javaObjects.txt"));
// write a date
objectOutputStream.writeObject(new Date());
// write a boolean
objectOutputStream.writeBoolean(true);
// write a float
objectOutputStream.writeFloat(1.0f);
// the other primitive types and objects can be saved as well
// create two students objects and add them in a list. create a course
// object and add the list of students to a list
Student student1 = new Student();
student1.setAge(30);
student1.setName("Student1");
Student student2 = new Student();
student2.setAge(31);
student2.setName("Student2");
Course course = new Course();
course.setName("Java IO");
List<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
course.setStudents(students);
// write the course object to the objectoutputstream. This writes the
// object as well all objects that it referes to.
// it writes only those objects that implement serializable
objectOutputStream.writeObject(course);
objectOutputStream.flush();
objectOutputStream.close();
// the object input stream reads the objects back from the file and
// creates java objects out of them. It recreates all
// references that were present when the objects were written
ObjectInputStream objectInputStream = new ObjectInputStream(
new FileInputStream("javaObjects.txt"));
// start getting the objects out in the order in which they were written
Date date = (Date) objectInputStream.readObject();
System.out.println(date);
System.out.println(objectInputStream.readBoolean());
System.out.println(objectInputStream.readFloat());
// get the course object
Course readCourse = (Course) objectInputStream.readObject();
System.out.println(readCourse.getName());
Student student1Read = readCourse.getStudents().get(0);
System.out.println(student1Read.getAge());
System.out.println(student1Read.getName());
objectInputStream.close();
?
The Student Class
Create an object output stream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Student implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Create an object output stream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Course implements Serializable {
String name;
List<Student> students;
public void setName(String name) {
this.name = name;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public String getName() {
return name;
}
public List<Student> getStudents() {
return students;
}
}
(http://www.studytrails.com/java-io/character-file-reading-writing.jsp)Previous
Next (http://www.studytrails.com/java-io/randomAccessFile.jsp)
?
?
RServe Java Source R script file 3 comments Java - org.json 1 comment
Java json jackson Mix-In Annotation 1 comment Java API for XML (JAXP) - XSLT
Transformation 2 comments
ALSO ON STUDYTRAILS
0 Comments StudyTrails Login

Sort by Best Share

Start the discussion


WHAT'S THIS?
Subscribe

Add Disqus to your site


d
Favorite

Sitemap (http://www.studytrails.com/sitemap.xml)
Copyright StudyTrails 2012-2014

Vous aimerez peut-être aussi