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: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  ww  .  j a va  2s  .c  om*/
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print from index 0 to 7
    byte[] b = new byte[13];
    ois.readFully(b, 0, 7);
    String array = new String(b);
    System.out.println(array);
    ois.close();

}

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 ava  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 Exception {
    User a = new User("A", "B");
    System.out.println("logon a = " + a);
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("User.out"));
    o.writeObject(a);/*from  w w  w  .j av a  2s.co  m*/
    o.close();

    Thread.sleep(1000); // Delay for 1 second

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("User.out"));
    System.out.println("Recovering object at " + new Date());
    a = (User) in.readObject();
    System.out.println("logon a = " + a);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    User admin = new User();
    admin.setId(new Long(1));
    User foo = new User();
    foo.setId(new Long(2));

    ObjectOutputStream oos = new ObjectOutputStream(
            new GZIPOutputStream(new FileOutputStream(new File("user.dat"))));
    oos.writeObject(admin);/*  w  ww  . ja va2  s .co m*/
    oos.writeObject(foo);
    oos.flush();
    oos.close();
}

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);//from w  w  w  . j a  va2s .co m
        ostream.close();
        sock.close();
    }
}

From source file:Main.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);/*w ww  .ja  v a  2  s  .  c o m*/
    oos.close();

    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 {
    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  w  w . j av a2  s  . c o m

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

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);/*from  w w  w.j av  a 2  s .  c o m*/
    oos.close();

    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: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);//from w  w  w  .ja va 2s.c om
    oos.close();

    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";
    byte[] b = { 'e', 'x', 'a', 'm', 'p', 'l', 'e' };

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

    // write something in the file
    oout.writeObject(s);// w  ww .j  a v a 2 s . c  o m
    oout.writeObject(b);
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print an object and cast it as string
    System.out.println((String) ois.readObject());

    // read and print an object and cast it as string
    byte[] read = (byte[]) ois.readObject();
    String s2 = new String(read);
    System.out.println(s2);
    ois.close();
}