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: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.j av  a  2 s.  co 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: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   www .j  a  v a2 s  .  co 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:Main.java

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

    byte b = 127;

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

    // write something in the file
    oout.writeByte(b);/*from  w w  w.  j  a  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 the unshared object
    System.out.println(ois.readUnsignedByte());
    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 ww.ja  va2 s.  c  o  m
    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[] args) throws Exception {
    short b = 32767;

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

    // write something in the file
    oout.writeShort(b);/* w  ww  .j  av  a 2s  .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 the short
    System.out.println(ois.readUnsignedShort());
    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   ww w .j a va 2s .  c  om
    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:MainClass.java

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

    AccountRecordSerializable record;//w  w w  . j a v  a2  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();
}

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);/* w  w w  .j  a  va  2 s  .c  o m*/
    oos.flush();
    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: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);//www. j  a va2s . 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 {

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

    oout.writeObject(new Example());
    oout.flush();//from w  w  w.j a  v  a2  s . c o  m
    oout.close();

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

    Example a = (Example) ois.readObject();

    System.out.println(a.isDefault);

    System.out.println(a.string);

    ois.close();
}