Example usage for java.io ObjectOutputStream ObjectOutputStream

List of usage examples for java.io ObjectOutputStream ObjectOutputStream

Introduction

In this page you can find the example usage for java.io ObjectOutputStream ObjectOutputStream.

Prototype

public ObjectOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates an ObjectOutputStream that writes to the specified OutputStream.

Usage

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);//from w ww .  j  ava 2 s .c o  m
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.writeFields();

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);/*  www .j av a 2  s  .  c  o m*/
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    ObjectOutputStream.PutField putField = outputStream.putFields();

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);/*from  w  ww. j  a  va  2  s .c  o m*/
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.flush();

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);//from ww w.ja  v  a  2s  .  c  o m
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.reset();

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = null;

    outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);/*from  ww w  .java  2  s.c om*/
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);
}

From source file:Employee.java

public static void main(String[] args) throws Exception {
    Employee e1 = new Employee("A", 45000.0);
    System.out.println(e1.getName() + " " + e1.getSalary());

    FileOutputStream fos = new FileOutputStream("employee.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(e1);/*  w w  w . java 2 s. co m*/

    FileInputStream fis = new FileInputStream("employee.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);

    Employee e2 = (Employee) ois.readObject();
    System.out.println(e2.getName() + " " + e2.getSalary());

}

From source file:A.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    A b1 = new A();
    B b2 = new B();
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("File.out"));
    o.writeObject(b1);/*  w  ww.  j av  a 2s.  c om*/
    o.writeObject(b2);
    o.close();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("File.out"));
    b1 = (A) in.readObject();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);/*from w  ww.j  a  v  a  2s  .  co m*/
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1);

    outputStream.close();
}

From source file:A.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    A b1 = new A();
    B b2 = new B();
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("File.out"));
    System.out.println("Saving objects:");
    o.writeObject(b1);//ww w .  j a va  2  s .  com
    o.writeObject(b2);
    o.close();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("File.out"));
    System.out.println("Recovering b1:");
    b1 = (A) in.readObject();
}

From source file:House.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    House house = new House();
    List animals = new ArrayList();
    animals.add(new Animal("Bosco the dog", house));
    animals.add(new Animal("Ralph the hamster", house));
    animals.add(new Animal("Fronk the cat", house));
    System.out.println("animals: " + animals);
    ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
    ObjectOutputStream o1 = new ObjectOutputStream(buf1);
    o1.writeObject(animals);//from   w ww.  j  a v a 2 s. com
    o1.writeObject(animals); // Write a 2nd set
    // Write to a different stream:
    ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
    ObjectOutputStream o2 = new ObjectOutputStream(buf2);
    o2.writeObject(animals);
    // Now get them back:
    ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));
    ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
    List animals1 = (List) in1.readObject(), animals2 = (List) in1.readObject(),
            animals3 = (List) in2.readObject();
    System.out.println("animals1: " + animals1);
    System.out.println("animals2: " + animals2);
    System.out.println("animals3: " + animals3);
}