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

public static void main(String args[]) throws Exception {
    String[] a = new String[] { "a", "b", "c" };

    Vector v = new Vector(Arrays.asList());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(v);/*from   www .j ava2s  .co  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:SaveVector.java

public static void main(String args[]) throws Exception {
    String data[] = { "Java", "Source", "and", "Support", "." };
    Vector v = new Vector(Arrays.asList(data));
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(v);// ww w . j  a va2s. c o  m
    o.close();
    ByteArrayInputStream bb = new ByteArrayInputStream(b.toByteArray());
    ObjectInputStream oo = new ObjectInputStream(bb);
    Vector v2 = (Vector) oo.readObject();
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    MyBean sc = new MyBean("Test1", "Test2");
    System.out.println("Before:\n" + sc);

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(buf);
    o.writeObject(sc);/* w  w w  . ja  v a 2 s.c  om*/

    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
    MyBean sc2 = (MyBean) in.readObject();
    System.out.println("After:\n" + sc2);
}

From source file:Main.java

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

    String s = "Hello World from java2s.com";

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

    oout.writeObject(s);/*  w w w .  j av  a 2 s.  c om*/
    oout.flush();
    oout.close();
    Main ois = new Main(new FileInputStream("test.txt"));

    System.out.println((String) ois.readObjectOverride());
    ois.close();
}

From source file:Main.java

public static void main(String[] args) {
    int[] x = new int[] { 1, 2, 3 };
    int[] y = new int[] { 4, 5, 6 };
    Polygon polygon = new Polygon(x, y, x.length);
    try {// w  w w . j a v  a 2s  . c  o  m
        ObjectOutputStream objectOut = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream("Polygons.bin")));
        objectOut.writeObject(polygon);
        objectOut.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    try {
        ObjectInputStream objectIn = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream("Polygons.bin")));
        Polygon theLine = (Polygon) objectIn.readObject();
        System.out.println(theLine);
        objectIn.close();
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

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

    String s = "Hello World from java2s.com";

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

    // write something in the file
    oout.writeUTF(s);/*from w w w  .  j av  a 2 s .c o m*/
    oout.flush();

    // create an ObjectInputStream for the file we created before
    Main ois = new Main(new FileInputStream("test.txt"));

    // enable object resolving
    ois.enableResolveObject(true);

    // get the class for string and print the name
    System.out.println(ois.resolveObject(ois.readUTF()));
    ois.close();
}

From source file:Main.java

public static void main(String args[]) throws IOException, ClassNotFoundException {
    File file = new File("test.txt");
    FileOutputStream outFile = new FileOutputStream(file);
    ObjectOutputStream outStream = new ObjectOutputStream(outFile);
    TestClass1 t1 = new TestClass1(true, 9, 'A', 0.0001, "java");
    TestClass2 t2 = new TestClass2();
    String t3 = "This is a test.";
    Date t4 = new Date();
    outStream.writeObject(t1);//from   w w  w.  j av a  2s  . c  o  m
    outStream.writeObject(t2);
    outStream.writeObject(t3);
    outStream.writeObject(t4);
    outStream.close();
    outFile.close();
    FileInputStream inFile = new FileInputStream(file);
    ObjectInputStream inStream = new ObjectInputStream(inFile);
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    inStream.close();
    inFile.close();
    file.delete();
}

From source file:ObjServer.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Hashtable hash = new Hashtable();
    hash.put("Java Source and Support", "www.java2s.com");

    while (true) {
        System.out.println("Listening");
        Socket sock = ssock.accept();

        ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream());
        ostream.writeObject(hash);// w  ww.  j av  a 2  s . c  om
        ostream.close();
        sock.close();
    }
}

From source file:Main.java

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

    String s = "Hello World from java2s.com";

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

    oout.writeUTF(s);/*  w  ww .  j  ava  2 s  .  c o  m*/
    oout.flush();
    oout.close();

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

    String[] list = { Serializable.class.getName() };

    System.out.println(ois.resolveProxyClass(list));
    ois.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    MyBean myBean = new MyBean("A String ", 47);
    System.out.println(myBean);//from  w w w  . j  a v a2 s . c o m
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("MyBean.out"));
    System.out.println("Saving object:");
    o.writeObject(myBean);
    o.close();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("MyBean.out"));
    System.out.println("Recovering:");
    myBean = (MyBean) in.readObject();
    System.out.println(myBean);
}