Example usage for java.io ObjectInputStream readObject

List of usage examples for java.io ObjectInputStream readObject

Introduction

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

Prototype

public final Object readObject() throws IOException, ClassNotFoundException 

Source Link

Document

Read an object from the ObjectInputStream.

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 w  w.ja  va2  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:shutdown.java

License:asdf

public static void main(String[] args) throws ClassNotFoundException, IOException {
    FileOutputStream fos = new FileOutputStream("objects.tmp");

    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject("asdf");
    oos.flush();//from w w w  .j a  v a2  s .c  om
    fos.close();

    FileInputStream fis = new FileInputStream("objects.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    String t = (String) ois.readObject();
    fis.close();

}

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);/*from w ww.  j a v  a2 s .  c om*/
    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: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);//from  w w  w  .j a  va2s . 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:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    MessageDigest md = MessageDigest.getInstance("SHA");
    DigestInputStream dis = new DigestInputStream(fis, md);
    ObjectInputStream ois = new ObjectInputStream(dis);
    Object o = ois.readObject();
    if (!(o instanceof String)) {
        System.out.println("Unexpected data in file");
        System.exit(-1);/*from www . j a  va  2 s  . c  om*/
    }
    String data = (String) o;
    System.out.println("Got message " + data);
    dis.on(false);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    System.out.println(MessageDigest.isEqual(md.digest(), origDigest));
}

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);/*w  w w.  j  a v  a2  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:MainClass.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object o = ois.readObject();
    if (!(o instanceof String)) {
        System.out.println("Unexpected data in file");
        System.exit(-1);//from  w  w  w. j  a v a  2 s .  c o m
    }
    String data = (String) o;
    System.out.println("Got message " + data);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(data.getBytes());
    if (MessageDigest.isEqual(md.digest(), origDigest))
        System.out.println("Message is valid");
    else
        System.out.println("Message was corrupted");
}

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  ww w  .  ja v a 2  s.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: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   w w  w . j a  va2  s  . com
    oos.close();

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Hashtable h = new Hashtable();
    h.put("string", "AAA");
    h.put("int", new Integer(26));
    h.put("double", new Double(Math.PI));

    FileOutputStream fileOut = new FileOutputStream("hashtable.ser");
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(h);/*from  w  ww .ja  va  2s.c  o  m*/

    FileInputStream fileIn = new FileInputStream("h.ser");
    ObjectInputStream in = new ObjectInputStream(fileIn);
    Hashtable h = (Hashtable) in.readObject();
    System.out.println(h.toString());
}