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

public static void main(String[] args) throws Exception {
    ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("clients.ser"));

    AccountRecordSerializable record;//from  w  ww .  jav  a 2s  .  co  m

    record = new AccountRecordSerializable(1, "firstName", "lastName", 0.1);
    output.writeObject(record);

    output.close();
}

From source file:Main.java

public static void main(String[] args) {
    Card card = new Card();
    try {//from  w  w w  . jav a2 s .c o  m
        FileOutputStream out = new FileOutputStream("card.out");
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(card);
        oos.flush();
        oos.close();
    } catch (Exception e) {
        System.out.println("Problem serializing: " + e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeBoolean(true);// w w w  .  ja va  2 s  .  co  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 a boolean
    System.out.println(ois.readBoolean());
    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.writeUTF("Hello World from java2s.com");
    oout.flush();//from  w w w  . ja  v  a 2s  .co  m
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    for (int i = 0; i < ois.available();) {
        System.out.print((char) ois.read());
    }
    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);

    // write something in the file
    oout.writeUTF("Hello World from java2s.com");
    oout.flush();//from   w  w w . j  a v  a  2s .  com
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // check how many bytes are available
    System.out.println(ois.available());
    ois.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object object = new JButton("push me");

    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
    out.writeObject(object);//w  ww .j a va 2s.  co  m
    out.close();

    // Serialize to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    out = new ObjectOutputStream(bos);
    out.writeObject(object);
    out.close();

    // Get the bytes of the serialized object
    byte[] buf = bos.toByteArray();
    System.out.println(new String(buf));
}

From source file:Main.java

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

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

    // write something in the file
    oout.writeUTF("Hello World from java2s.com");
    oout.flush();//from   w w w  .jav a  2  s  . c  o  m

    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read from the stream
    for (int i = 0; i < ois.available();) {
        System.out.print((char) ois.read());
    }

    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);/*  ww  w  .java  2 s  . c om*/
    oos.writeObject(data);
    oos.writeObject(md.digest());
}

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 w w w  .  ja  v  a 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]);

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("clients.ser"));

    AccountRecordSerializable record;/*from w  ww  . ja  va  2 s .  c o m*/

    record = new AccountRecordSerializable(1, "firstName", "lastName", 0.1);
    output.writeObject(record);

    ObjectInputStream input = new ObjectInputStream(new FileInputStream("clients.ser"));
    record = (AccountRecordSerializable) input.readObject();

    System.out.printf("%-10d%-12s%-12s%10.2f\n", record.getAccount(), record.getFirstName(),
            record.getLastName(), record.getBalance());

    output.close();
}