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:Copy.java

public static void main(String args[]) throws Exception {
    String elements[] = { "Irish Setter", "Poodle", "English Setter", "Gordon Setter", "Pug" };
    Set set = new HashSet(Arrays.asList(elements));
    Set set2 = ((Set) ((HashSet) set).clone());
    System.out.println(set2);//from  w  ww  . java2 s  .  c  o m
    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);
    oos.close();
    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set set3 = (Set) ois.readObject();
    ois.close();
    System.out.println(set3);
}

From source file:SaveVector1.java

public static void main(String args[]) throws Exception {
    Vector v = new Vector(Arrays.asList(args));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(v);/*  w w w .ja v a 2 s .  c  o  m*/
    oos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    Vector v2 = (Vector) ois.readObject();
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));

    out.writeObject(Calendar.getInstance());
    out.writeObject(new BigDecimal("123.123"));
    out.writeInt(1);// www .java  2 s. c om
    out.writeUTF("tutorial");
    out.close();

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    BigDecimal price;
    int unit;
    String desc;

    Calendar date = (Calendar) in.readObject();
    System.out.println(date);

    price = (BigDecimal) in.readObject();
    unit = in.readInt();
    desc = in.readUTF();
    System.out.println(unit);
    System.out.println(desc);
    System.out.println(price);
    in.close();
}

From source file:FreezeAlien.java

public static void main(String[] args) throws Exception {
    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("X.file"));
    Alien zorcon = new Alien();
    out.writeObject(zorcon);/*ww w . j  ava2  s  .com*/
}

From source file:MainClass.java

public static void main(String[] args) {
    Employee e = new Employee();
    e.name = "Jor Lee";
    e.address = "USA";
    e.SSN = 11122333;// w  w w .j  a va2  s .  com
    e.number = 101;

    try {
        FileOutputStream fileOut = new FileOutputStream("employee.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);

        out.writeObject(e);

        out.close();
        fileOut.close();
    } catch (IOException i) {
        i.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    oout.writeObject(new Example());
    oout.flush();/*  www. j  a  v  a2s. c  o m*/
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    Example a = (Example) ois.readObject();

    System.out.println(a.isDefault);

    System.out.println(a.string);

    ois.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Vector v = new Vector(Arrays.asList("a", "b", "c"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(v);/*from   w w w  . ja v a2s.c  o  m*/
    oos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    Vector v2 = (Vector) ois.readObject();
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    oout.writeObject(new Example());
    oout.flush();//www . j a v  a2  s .co  m
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    Example a = (Example) ois.readObject();

    System.out.println(a.s);

    a.validateObject();
    ois.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Junk obj1 = new Junk("A");
    Junk obj2 = new Junk("B");
    Junk obj3 = new Junk("V");
    ObjectOutputStream objectOut = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("C:/JunkObjects.bin")));

    objectOut.writeObject(obj1); // Write object
    objectOut.writeObject(obj2); // Write object
    objectOut.writeObject(obj3); // Write object
    objectOut.close(); // Close the output stream

    ObjectInputStream objectIn = null;
    int objectCount = 0;
    Junk object = null;//from www.ja  va2 s  .c om

    objectIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream("C:/JunkObjects.bin")));

    // Read from the stream until we hit the end
    while (objectCount < 3) {
        object = (Junk) objectIn.readObject();
        objectCount++;
        System.out.println(object);
    }

    objectIn.close();

}

From source file:MainClass.java

public static void main(String[] a) throws Exception {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set set = new HashSet(Arrays.asList(elements));

    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);//from ww w.  j a  va  2s.c o m
    oos.close();

    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set anotherSet = (Set) ois.readObject();
    ois.close();
    System.out.println(anotherSet);
}