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

public static void main(String[] args) throws Exception {
    Connection con;//from w  ww  .  j av a2 s .c  om
    con = DriverManager.getConnection("jdbc:derby://localhost:1527/" + "c:\\db\\employee");

    PreparedStatement ps;
    ps = con.prepareStatement("insert into employee(name,photo) " + "values(?,?)");
    ps.setString(1, "Duke");

    Blob blob = con.createBlob();
    ImageIcon ii = new ImageIcon("duke.png");

    ObjectOutputStream oos;
    oos = new ObjectOutputStream(blob.setBinaryStream(1));
    oos.writeObject(ii);
    oos.close();
    ps.setBlob(2, blob);
    ps.execute();
    blob.free();
    ps.close();
}

From source file:CachedRS.java

public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(CRS_FILE_LOC);
    ObjectInputStream in = new ObjectInputStream(fis);
    CachedRowSet crs = (CachedRowSet) in.readObject();
    fis.close();/*from w  w  w. ja v a  2  s .c  om*/
    in.close();

    Class.forName("oracle.jdbc.driver.OracleDriver");
    crs.setUrl("jdbc:oracle:thin:@localhost:1521:ORCL");
    crs.setUsername("yourName");
    crs.setPassword("mypwd");
    String sql = "SELECT SSN, Name, Salary, Hiredate FROM Employees WHERE SSN=?";
    crs.setCommand(sql);
    crs.setInt(1, 111111111);
    crs.execute();

    FileOutputStream fos = new FileOutputStream(CRS_FILE_LOC);
    ObjectOutputStream out = new ObjectOutputStream(fos);
    out.writeObject(crs);
    out.close();
    crs.close();

    fis = new FileInputStream(CRS_FILE_LOC);
    in = new ObjectInputStream(fis);
    crs = (CachedRowSet) in.readObject();
    fis.close();
    in.close();

    while (crs.next()) {
        System.out.print("SSN: " + crs.getInt("ssn"));
        System.out.print(", Name: " + crs.getString("name"));
        System.out.print(", Salary: $" + crs.getDouble("salary"));
        System.out.print(", HireDate: " + crs.getDate("hiredate"));
    }
    crs.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);
    o.close();/*from w w  w .j  av  a2s .  c o m*/

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

public static void main(String[] args) {
    Employee e = new Employee();
    e.name = "A";
    e.address = "B";
    e.SSN = 11111;/*ww  w  . j  a  va2  s  .  c o m*/
    e.number = 101;
    try {
        FileOutputStream fileOut = new FileOutputStream("employee.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(e);
        out.close();
        fileOut.close();
    } catch (IOException i) {
        i.printStackTrace();
    }
}

From source file:A.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    A b1 = new A();
    B b2 = new B();
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("File.out"));
    o.writeObject(b1);
    o.writeObject(b2);/*  w ww. j a  va 2 s.c o  m*/
    o.close();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("File.out"));
    b1 = (A) in.readObject();
}

From source file:SimpleSocketServer.java

  public static void main(String args[]) throws Exception {
  ServerSocket serverSocket;// ww  w .ja  v  a 2 s  . co  m
  int portNumber = 1777;
  Socket socket;
  String str;

  str = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  str += "<ticketRequest><customer custID=\"1\">";
  str += "</ticketRequest>";

  serverSocket = new ServerSocket(portNumber);

  System.out.println("Waiting for a connection on " + portNumber);

  socket = serverSocket.accept();

  ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

  oos.writeObject(str);

  oos.close();

  socket.close();

}

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);
    oout.writeObject(b);//from   www  . ja  va 2  s. c  o m
    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();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("books.dat");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    Book book = new Book("1", "Java", "A");

    oos.writeObject(book);
    oos.flush();/* www .ja  v a  2  s . co  m*/
    oos.close();
    FileInputStream fis = new FileInputStream("books.dat");
    ObjectInputStream ois = new ObjectInputStream(fis);
    book = (Book) ois.readObject();
    System.out.println(book.toString());

    ois.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileOutputStream fos = new FileOutputStream("test");
    MessageDigest md = MessageDigest.getInstance("SHA");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    String data = "thee";
    byte buf[] = data.getBytes();
    md.update(buf);/*  w ww.  j  a  v  a  2  s .co m*/
    oos.writeObject(data);
    oos.writeObject(md.digest());
}

From source file:com.textocat.textokit.morph.opencorpora.resource.XmlDictionaryParserLauncher.java

public static void main(String[] args) throws Exception {
    XmlDictionaryParserLauncher cfg = new XmlDictionaryParserLauncher();
    new JCommander(cfg, args);

    MorphDictionaryImpl dict = new MorphDictionaryImpl();
    DictionaryExtension ext = cfg.dictExtensionClass.newInstance();
    FileInputStream fis = FileUtils.openInputStream(cfg.dictXmlFile);
    try {//w  w w.j  a va  2 s . c  om
        new XmlDictionaryParser(dict, ext, fis).run();
    } finally {
        IOUtils.closeQuietly(fis);
    }

    log.info("Preparing to serialization...");
    long timeBefore = currentTimeMillis();
    OutputStream fout = new BufferedOutputStream(FileUtils.openOutputStream(cfg.outputFile), 8192 * 8);
    ObjectOutputStream out = new ObjectOutputStream(fout);
    try {
        out.writeObject(dict.getGramModel());
        out.writeObject(dict);
    } finally {
        out.close();
    }
    log.info("Serialization finished in {} ms.\nOutput size: {} bytes", currentTimeMillis() - timeBefore,
            cfg.outputFile.length());
}