Example usage for java.io ObjectInputStream readObject

List of usage examples for java.io ObjectInputStream readObject

Introduction

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

Prototype

public final Object readObject() throws IOException, ClassNotFoundException 

Source Link

Document

Read an object from the ObjectInputStream.

Usage

From source file:LoopingSocketServer.java

public static void main(String args[]) throws Exception {
    ServerSocket servSocket;/*w  w  w  .  j a va  2 s  . c  o  m*/
    Socket fromClientSocket;
    int cTosPortNumber = 1777;
    String str;

    servSocket = new ServerSocket(cTosPortNumber);
    System.out.println("Waiting for a connection on " + cTosPortNumber);
    fromClientSocket = servSocket.accept();
    System.out.println("fromClientSocket accepted");

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

    ObjectInputStream ois = new ObjectInputStream(fromClientSocket.getInputStream());

    while ((str = (String) ois.readObject()) != null) {
        System.out.println("The message from client:  " + str);

        if (str.equals("bye")) {
            oos.writeObject("bye bye");
            break;
        } else {
            str = "Server returns " + str;
            oos.writeObject(str);
        }

    }
    oos.close();
    fromClientSocket.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    MyBean sc = new MyBean("Test1", "Test2");
    System.out.println("Before:\n" + sc);

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(buf);
    o.writeObject(sc);//from   w w w . j a  v a  2 s. com

    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
    MyBean sc2 = (MyBean) in.readObject();
    System.out.println("After:\n" + sc2);
}

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 ww  . ja  va  2 s  .co 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();
}

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();// ww  w.jav  a2 s  . co m

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

From source file:Main.java

public static void main(String args[]) throws IOException, ClassNotFoundException {
    File file = new File("test.txt");
    FileOutputStream outFile = new FileOutputStream(file);
    ObjectOutputStream outStream = new ObjectOutputStream(outFile);
    TestClass1 t1 = new TestClass1(true, 9, 'A', 0.0001, "java");
    TestClass2 t2 = new TestClass2();
    String t3 = "This is a test.";
    Date t4 = new Date();
    outStream.writeObject(t1);/* w w w .  j  a v  a 2  s  .c o m*/
    outStream.writeObject(t2);
    outStream.writeObject(t3);
    outStream.writeObject(t4);
    outStream.close();
    outFile.close();
    FileInputStream inFile = new FileInputStream(file);
    ObjectInputStream inStream = new ObjectInputStream(inFile);
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    inStream.close();
    inFile.close();
    file.delete();
}

From source file:A.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    A b1 = new A();
    B b2 = new B();
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("File.out"));
    System.out.println("Saving objects:");
    o.writeObject(b1);/*from w w w  .ja  v a2  s  .c  o m*/
    o.writeObject(b2);
    o.close();

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

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    random.setSeed(101L);/*w ww  .  j  a  v  a 2  s  . c  o m*/
    keyGen.initialize(1024, random);
    KeyPair keypair = keyGen.generateKeyPair();

    KeyFactory kfactory = KeyFactory.getInstance("DSA");

    DSAPublicKeySpec kspec = (DSAPublicKeySpec) kfactory.getKeySpec(keypair.getPublic(),
            DSAPublicKeySpec.class);

    System.out.println(keypair.getPublic());
    FileOutputStream fos = new FileOutputStream("publicKeys");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(kspec.getY());
    oos.writeObject(kspec.getP());
    oos.writeObject(kspec.getQ());
    oos.writeObject(kspec.getG());

    FileInputStream fin = new FileInputStream("publicKeys");
    ObjectInputStream ois = new ObjectInputStream(fin);

    BigInteger Y = (BigInteger) ois.readObject();
    BigInteger P = (BigInteger) ois.readObject();
    BigInteger Q = (BigInteger) ois.readObject();
    BigInteger G = (BigInteger) ois.readObject();

    DSAPublicKeySpec keyspec = new DSAPublicKeySpec(Y, P, Q, G);
    PublicKey pkey = kfactory.generatePublic(keyspec);
    System.out.println(pkey);
}

From source file:MyClass.java

public static void main(String[] argv) throws Exception {
    double v[] = { 1.1, 2.2, 3.3 };
    double v2[] = { 9.0, 8.0, 7.7 };

    MyClass obj1 = new MyClass("This is a test", v, "Test.txt");
    MyClass obj2 = new MyClass("Alpha Beta Gamma", v2, "Sample.dat");

    ObjectOutputStream fout = new ObjectOutputStream(new FileOutputStream("obj.dat"));
    System.out.println("obj1:\n" + obj1);
    fout.writeObject(obj1);/*from   w w  w . j  av  a 2s . c o m*/
    System.out.println("obj2:\n" + obj2);
    fout.writeObject(obj2);
    fout.close();

    ObjectInputStream fin = new ObjectInputStream(new FileInputStream("obj.dat"));
    MyClass inputObj;

    inputObj = (MyClass) fin.readObject();
    System.out.println("First object:\n" + inputObj);

    inputObj = (MyClass) fin.readObject();
    System.out.println("Second object:\n" + inputObj);
    fin.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 www  . j  a  va  2 s.  com
    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:net.sf.jabb.util.text.test.KeywordMatcherExample.java

/**
 * @param args//from  w  w w  . ja  v a  2 s. c om
 * @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);

}