Example usage for java.io ObjectInputStream ObjectInputStream

List of usage examples for java.io ObjectInputStream ObjectInputStream

Introduction

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

Prototype

public ObjectInputStream(InputStream in) throws IOException 

Source Link

Document

Creates an ObjectInputStream that reads from the specified InputStream.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbname";
    Connection dbConn = DriverManager.getConnection(URL, "user", "passw");
    Employee employee = new Employee(42, "AA", 9);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(employee);/*from   w w w. ja v  a2  s .  c  om*/
    byte[] employeeAsBytes = baos.toByteArray();
    PreparedStatement pstmt = dbConn.prepareStatement("INSERT INTO EMPLOYEE (emp) VALUES(?)");
    ByteArrayInputStream bais = new ByteArrayInputStream(employeeAsBytes);
    pstmt.setBinaryStream(1, bais, employeeAsBytes.length);
    pstmt.executeUpdate();
    pstmt.close();
    Statement stmt = dbConn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT emp FROM Employee");
    while (rs.next()) {
        byte[] st = (byte[]) rs.getObject(1);
        ByteArrayInputStream baip = new ByteArrayInputStream(st);
        ObjectInputStream ois = new ObjectInputStream(baip);
        Employee emp = (Employee) ois.readObject();
    }
    stmt.close();
    rs.close();
    dbConn.close();
}

From source file:Data.java

public static void main(String[] args) {
    Data data = new Data(1);
    try {/*from w w  w.ja  va  2 s . c om*/
        ObjectOutputStream objectOut = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream("C:/test.bin")));
        objectOut.writeObject(data);
        System.out.println("1st Object written has value: " + data.getValue());
        data.setValue(2);
        objectOut.writeObject(data);
        System.out.println("2nd Object written has value: " + data.getValue());
        data.setValue(3);
        objectOut.writeObject(data);
        System.out.println("3rd Object written has value: " + data.getValue());
        objectOut.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    try {
        ObjectInputStream objectIn = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream("C:/test.bin")));
        Data data1 = (Data) objectIn.readObject();
        Data data2 = (Data) objectIn.readObject();
        Data data3 = (Data) objectIn.readObject();
        System.out.println(data1.equals(data2));
        System.out.println(data2.equals(data3));
        System.out.println(data1.getValue());
        System.out.println(data2.getValue());
        System.out.println(data3.getValue());
        objectIn.close();
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

From source file:House.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    House house = new House();
    List animals = new ArrayList();
    animals.add(new Animal("Bosco the dog", house));
    animals.add(new Animal("Ralph the hamster", house));
    animals.add(new Animal("Fronk the cat", house));
    System.out.println("animals: " + animals);
    ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
    ObjectOutputStream o1 = new ObjectOutputStream(buf1);
    o1.writeObject(animals);//from   w  ww.j a va  2 s  .c  o m
    o1.writeObject(animals); // Write a 2nd set
    // Write to a different stream:
    ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
    ObjectOutputStream o2 = new ObjectOutputStream(buf2);
    o2.writeObject(animals);
    // Now get them back:
    ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));
    ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
    List animals1 = (List) in1.readObject(), animals2 = (List) in1.readObject(),
            animals3 = (List) in2.readObject();
    System.out.println("animals1: " + animals1);
    System.out.println("animals2: " + animals2);
    System.out.println("animals3: " + animals3);
}

From source file:net.sf.jabb.util.text.test.KeywordMatcherExample.java

/**
 * @param args/*  w w  w .j  a v a 2 s.co m*/
 * @throws IOException 
 * @throws ClassNotFoundException 
 */
public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("====  ====");
    KeywordMatcher m = showExample(null);

    System.out.println("==== ? ====");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(m);
    byte[] binary = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(binary);
    ObjectInputStream ois = new ObjectInputStream(bais);
    KeywordMatcher m2 = (KeywordMatcher) ois.readObject();
    showExample(m2);

    System.out.println("==== ? ====");
    KeywordMatcher m3 = new KeywordMatcher(m);
    showExample(m3);

}

From source file:Blip1.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    Blip1 b1 = new Blip1();
    Blip2 b2 = new Blip2();
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blips.out"));
    System.out.println("Saving objects:");
    o.writeObject(b1);/*www. j  ava2  s.  c  o m*/
    o.writeObject(b2);
    o.close();
    // Now get them back:
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blips.out"));
    System.out.println("Recovering b1:");
    b1 = (Blip1) in.readObject();
    // OOPS! Throws an exception:
    //! System.out.println("Recovering b2:");
    //! b2 = (Blip2)in.readObject();
}

From source file:Blip3.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    Blip3 b3 = new Blip3("A String ", 47);
    System.out.println(b3);//www  .jav  a  2s.  c  om
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blip3.out"));
    System.out.println("Saving object:");
    o.writeObject(b3);
    o.close();
    // Now get it back:
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blip3.out"));
    System.out.println("Recovering b3:");
    b3 = (Blip3) in.readObject();
    System.out.println(b3);
}

From source file:PersonExt.java

public static void main(String[] args) {
    PersonExt p1 = new PersonExt("John", "Male", 6.7);
    PersonExt p2 = new PersonExt("Wally", "Male", 5.7);
    PersonExt p3 = new PersonExt("Katrina", "Female", 5.4);

    File fileObject = new File("personext.ser");

    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileObject))) {
        oos.writeObject(p1);//w ww  . ja  v  a 2  s. c  o  m
        oos.writeObject(p2);
        oos.writeObject(p3);

        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    fileObject = new File("personext.ser");

    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileObject))) {

        p1 = (PersonExt) ois.readObject();
        p2 = (PersonExt) ois.readObject();
        p3 = (PersonExt) ois.readObject();

        // Let's display the objects that are read
        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);

        // Print the input path
        System.out.println("Objects were  read   from  " + fileObject.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ObjectStreamTest.java

public static void main(String[] args) {
    Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    carl.setSecretary(harry);/*from  www .  j  av  a 2s  .co m*/
    Manager tony = new Manager("Tony Tester", 40000, 1990, 3, 15);
    tony.setSecretary(harry);

    Employee[] staff = new Employee[3];

    staff[0] = carl;
    staff[1] = harry;
    staff[2] = tony;

    try {
        // save all employee records to the file employee.dat
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));
        out.writeObject(staff);
        out.close();

        // retrieve all records into a new array
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));
        Employee[] newStaff = (Employee[]) in.readObject();
        in.close();

        // raise secretary's salary
        newStaff[1].raiseSalary(10);

        // print the newly read employee records
        for (Employee e : newStaff)
            System.out.println(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.couragelabs.logging.LogAppenderTestFixture.java

/**
 * Use this method to test the appender. Run this first, then run
 * GlobalContextSocketAppender::main//from w ww  . j ava  2s.  c  om
 *
 * @param args Program arguments. None are needed.
 * @throws java.lang.Exception if things go wrong
 */
@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(PORT);

    System.out.println("Starting listen loop.");
    while (true) {
        try {
            final Socket clientSocket = serverSocket.accept();
            System.out.println("Received client connection.");
            new Thread() {
                @Override
                public void run() {
                    ObjectInputStream i = null;
                    try {
                        i = new ObjectInputStream(clientSocket.getInputStream());
                        while (true) {
                            Object received = i.readObject();
                            System.out.println(ToStringBuilder.reflectionToString(received,
                                    ToStringStyle.SHORT_PREFIX_STYLE));
                            Thread.sleep(1000);
                        }
                    } catch (EOFException e) {
                        System.out.println("Client closed connection.");
                    } catch (Throwable t) {
                        t.printStackTrace();
                    } finally {
                        if (i != null) {
                            try {
                                i.close();
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                }
            }.start();
            Thread.sleep(1000);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        System.out.println("Next...");
    }

}

From source file:CardReader.java

public static void main(String[] args) {
    Card3 card = null;//from w ww  .j a v  a2s .  c  o m

    try {
        FileInputStream in = new FileInputStream("card.out");
        ObjectInputStream ois = new ObjectInputStream(in);
        card = (Card3) (ois.readObject());
    } catch (Exception e) {
        System.out.println("Problem serializing: " + e);
    }

    System.out.println("Card read is: " + card);

}