Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

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

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

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);
    oos.close();/*from w ww.  jav a  2 s  . c  om*/
    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:MainClass.java

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

    List list = Arrays.asList(new String[] { "A", "B", "C", "D" });

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

    FileInputStream fis = new FileInputStream("list.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    List anotherList = (List) ois.readObject();
    ois.close();

    System.out.println(anotherList);
}

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);
    oout.flush();/*from   ww w.  j a va 2 s.co  m*/
    oout.close();
    Main ois = new Main(new FileInputStream("test.txt"));

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

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    Set set = new HashSet(Arrays.asList(elements));
    Set set2 = ((Set) ((HashSet) set).clone());
    System.out.println(set2);//from w w w .  j a va  2  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:MainClass.java

public static void main(String args[]) throws Exception {
    SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
    SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 9999);
    socket.startHandshake();// www  .j a va2 s  .  c  om
    SSLSession session = socket.getSession();
    java.security.cert.Certificate[] servercerts = session.getPeerCertificates();

    List mylist = new ArrayList();
    for (int i = 0; i < servercerts.length; i++) {
        mylist.add(servercerts[i]);
    }

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    CertPath cp = cf.generateCertPath(mylist);

    FileOutputStream f = new FileOutputStream("CertPath.dat");
    ObjectOutputStream b = new ObjectOutputStream(f);
    b.writeObject(cp);

}

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();//  ww w .  j av a 2s . c  o  m
    oout.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

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

    System.out.println(a.s);
    ois.close();
}

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

    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 {
    ServerSocket ssock = new ServerSocket(1234);
    Hashtable hash = new Hashtable();
    hash.put("A", "a");

    while (true) {
        Socket sock = ssock.accept();
        ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream());
        ostream.writeObject(hash);
        ostream.close();/*from   ww  w  . j av a2  s .c  om*/
        sock.close();
    }
}

From source file:ObjectWriter.java

public static void main(String[] arguments) {
    Message mess = new Message();
    String author = "London";
    String recipient = "G, B";
    String[] letter = { "Merry Christmas." };
    Date now = new Date();
    mess.writeMessage(author, recipient, now, letter);
    try {/*  w w w. j a  v a 2  s.c  o m*/
        FileOutputStream fo = new FileOutputStream("Message.obj");
        ObjectOutputStream oo = new ObjectOutputStream(fo);
        oo.writeObject(mess);
        oo.close();
        System.out.println("Object created successfully.");
    } catch (IOException e) {
        System.out.println("Error - " + e.toString());
    }
}

From source file:InputOutputDemoObjectBinaryFile.java

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

    //Write an object or array to binary file "java2sObject.dat":
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("java2sObject.dat"));
    oos.writeObject(new int[] { 2, 3, 5, 7, 11 });
    oos.close();//from   ww  w. j a  va 2 s  . c o  m
    //Read objects or arrays from binary file "o.dat":
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("java2sObject.dat"));
    int[] ia = (int[]) (ois.readObject());
    System.out.println(ia[0] + "," + ia[1] + "," + ia[2] + "," + ia[3] + "," + ia[4]);

}