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) {
    File fileObject = new File("person.ser");

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

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

        System.out.println(p1);/*from   w w  w  .j a v a 2  s .c  o  m*/
        System.out.println(p2);
        System.out.println(p3);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("yourFile.dat"));

    Object obj = null;//  ww  w. j av  a  2  s.  c o  m

    while ((obj = inputStream.readObject()) != null) {
        if (obj instanceof Person) {
            System.out.println(((Person) obj).toString());
        }
    }
    inputStream.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ObjectInputStream ois = new ObjectInputStream(
            new GZIPInputStream(new FileInputStream(new File("user.dat"))));

    User admin = (User) ois.readObject();
    User foo = (User) ois.readObject();/*from   w  w w  .  j ava2 s .c o  m*/

    ois.close();
    System.out.println("Admin = [" + admin + "]");
    System.out.println("Foo = [" + foo + "]");
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object o = ois.readObject();/*from w w  w  .  j  a  va2 s  . co m*/
    if (!(o instanceof String)) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    String data = (String) o;
    System.out.println("Got message " + data);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(data.getBytes());
    if (MessageDigest.isEqual(md.digest(), origDigest))
        System.out.println("Message is valid");
    else
        System.out.println("Message was corrupted");
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object o = ois.readObject();//from w w w.  jav  a  2 s  .co m
    if (!(o instanceof String)) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    String data = (String) o;
    System.out.println("Got message " + data);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    byte pass[] = "aaa".getBytes();
    byte buf[] = data.getBytes();
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(pass);
    md.update(buf);
    byte digest1[] = md.digest();
    md.update(pass);
    md.update(digest1);
    System.out.println(MessageDigest.isEqual(md.digest(), origDigest));
}

From source file:ObjectReader.java

public static void main(String[] arguments) {
    try {// w w  w  . j a va2 s.  com
        FileInputStream fi = new FileInputStream("message.obj");
        ObjectInputStream oi = new ObjectInputStream(fi);
        Message mess = (Message) oi.readObject();
        System.out.println("Message:\n");
        System.out.println("From: " + mess.from);
        System.out.println("To: " + mess.to);
        System.out.println("Date: " + mess.when + "\n");
        for (int i = 0; i < mess.lineCount; i++)
            System.out.println(mess.text[i]);
        oi.close();
    } catch (Exception e) {
        System.out.println("Error " + e.toString());
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    MessageDigest md = MessageDigest.getInstance("SHA");
    DigestInputStream dis = new DigestInputStream(fis, md);
    ObjectInputStream ois = new ObjectInputStream(dis);
    Object o = ois.readObject();/*from w w  w.j  a v a 2s .c  om*/
    if (!(o instanceof String)) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    String data = (String) o;
    System.out.println("Got message " + data);
    dis.on(false);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    System.out.println(MessageDigest.isEqual(md.digest(), origDigest));
}

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  ww w.  j av a  2 s  .c  o m
    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 {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("keyfile"));
    DESKeySpec ks = new DESKeySpec((byte[]) ois.readObject());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey key = skf.generateSecret(ks);

    Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
    c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) ois.readObject()));
    CipherInputStream cis = new CipherInputStream(new FileInputStream("ciphertext"), c);
    BufferedReader br = new BufferedReader(new InputStreamReader(cis));
    System.out.println(br.readLine());
}

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  ww. ja  v  a2s .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();
}