java io - read and write java objects

Upload: phyzics

Post on 03-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Java IO - Read and Write Java Objects

    1/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.LikeLike ShareShare

    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)

    -

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

  • 8/11/2019 Java IO - Read and Write Java Objects

    2/4

    The Course Class

    Create an object output stream.

    12345678

    9101112131415161718192021222324

    25262728293031323334353637383940

    4142434445464748495051525354

    ObjectOutputStream objectOutputStream = new ObjectOutputStream( new FileOutputStream("javaObjects.txt"));// write a dateobjectOutputStream.writeObject(new Date());// write a booleanobjectOutputStream.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 listStudent 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 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 writtenObjectInputStream objectInputStream = new ObjectInputStream(

    new FileInputStream("javaObjects.txt"));// start getting the objects out in the order in which they were writtenDate date = (Date) objectInputStream.readObject();System.out.println(date);System.out.println(objectInputStream.readBoolean());System.out.println(objectInputStream.readFloat());

    // get the course objectCourse 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();

    ?

  • 8/11/2019 Java IO - Read and Write Java Objects

    3/4

    The Student Class

    Create an object output stream.

    12345678

    91011121314151617181920

    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.

    123456789

    1011121314

    151617181920

    public class Course implements Serializable { String name; List students;

    public void setName(String name) { this.name = name; }

    public void setStudents(List students) { this.students = students; }

    public String getName() { return name; }

    public List getStudents() { return students; }}

    (http://www.studytrails.com/java-io/character-file-reading-writing.jsp)Previous

    Next (http://www.studytrails.com/java-io/randomAccessFile.jsp)

    ?

    ?

  • 8/11/2019 Java IO - Read and Write Java Objects

    4/4