Example usage for java.io ObjectInputStream close

List of usage examples for java.io ObjectInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the input stream.

Usage

From source file:Main.java

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

    String s = "Hello World from java2s.com";

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

    // write something in the file
    oout.writeUTF(s);/*from  www .j  av  a 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 from index 0 to 7
    byte[] b = new byte[13];
    ois.readFully(b, 0, 7);
    String array = new String(b);
    System.out.println(array);
    ois.close();

}

From source file:ObjectStreamTest.java

public static void main(String[] args) {
    Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    carl.setSecretary(harry);//from  w ww .j  av  a2s. c  om
    Manager tony = new Manager("Tony Tester", 40000, 1990, 3, 15);
    tony.setSecretary(harry);

    Employee[] staff = new Employee[3];

    staff[0] = carl;
    staff[1] = harry;
    staff[2] = tony;

    try {
        // save all employee records to the file employee.dat
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));
        out.writeObject(staff);
        out.close();

        // retrieve all records into a new array
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));
        Employee[] newStaff = (Employee[]) in.readObject();
        in.close();

        // raise secretary's salary
        newStaff[1].raiseSalary(10);

        // print the newly read employee records
        for (Employee e : newStaff)
            System.out.println(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.msu.cme.rdp.graph.sandbox.CheckReadKmerPresence.java

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println("USAGE: CheckReadKmerPresence <bloom_filter> <nucl_seq_file>");
        System.exit(1);/*  w w  w  .j  av a2 s. com*/
    }

    File bloomFile = new File(args[0]);
    SeqReader reader = new SequenceReader(new File(args[1]));

    ObjectInputStream ois = ois = new ObjectInputStream(
            new BufferedInputStream(new FileInputStream(bloomFile)));
    BloomFilter filter = (BloomFilter) ois.readObject();
    ois.close();

    printStats(filter, System.out);
    Sequence seq;
    CodonWalker walker = null;
    while ((seq = reader.readNextSequence()) != null) {
        int kmerNum = 0;
        for (char[] kmer : KmerGenerator.getKmers(seq.getSeqString(), filter.getKmerSize())) {
            System.out.print(seq.getSeqName() + "\t" + (++kmerNum) + "\t" + kmer + "\t");
            try {
                walker = filter.new RightCodonFacade(kmer);
                System.out.println("true");
            } catch (Exception e) {
                System.out.println("false");
            }

        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String WRITE_OBJECT_SQL = "BEGIN " + "  INSERT INTO java_objects(object_id, object_name, object_value) "
            + "  VALUES (?, ?, empty_blob()) " + "  RETURN object_value INTO ?; " + "END;";
    String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE object_id = ?";

    Connection conn = getOracleConnection();
    conn.setAutoCommit(false);/*from  w w  w .j  a  v a  2  s.  com*/
    List<Object> list = new ArrayList<Object>();
    list.add("This is a short string.");
    list.add(new Integer(1234));
    list.add(new java.util.Date());

    // write object to Oracle
    long id = 0001;
    String className = list.getClass().getName();
    CallableStatement cstmt = conn.prepareCall(WRITE_OBJECT_SQL);

    cstmt.setLong(1, id);
    cstmt.setString(2, className);

    cstmt.registerOutParameter(3, java.sql.Types.BLOB);

    cstmt.executeUpdate();
    BLOB blob = (BLOB) cstmt.getBlob(3);
    OutputStream os = blob.getBinaryOutputStream();
    ObjectOutputStream oop = new ObjectOutputStream(os);
    oop.writeObject(list);
    oop.flush();
    oop.close();
    os.close();

    // Read object from oracle
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    InputStream is = rs.getBlob(1).getBinaryStream();
    ObjectInputStream oip = new ObjectInputStream(is);
    Object object = oip.readObject();
    className = object.getClass().getName();
    oip.close();
    is.close();
    rs.close();
    pstmt.close();
    conn.commit();

    // de-serialize list a java object from a given objectID
    List listFromDatabase = (List) object;
    System.out.println("[After De-Serialization] list=" + listFromDatabase);
    conn.close();
}

From source file:SignatureTest.java

public static void main(String[] args) {
    try {/*from ww  w  . j  av  a 2 s .  c o  m*/
        if (args[0].equals("-genkeypair")) {
            KeyPairGenerator pairgen = KeyPairGenerator.getInstance("DSA");
            SecureRandom random = new SecureRandom();
            pairgen.initialize(KEYSIZE, random);
            KeyPair keyPair = pairgen.generateKeyPair();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(keyPair.getPublic());
            out.close();
            out = new ObjectOutputStream(new FileOutputStream(args[2]));
            out.writeObject(keyPair.getPrivate());
            out.close();
        } else if (args[0].equals("-sign")) {
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            PrivateKey privkey = (PrivateKey) keyIn.readObject();
            keyIn.close();

            Signature signalg = Signature.getInstance("DSA");
            signalg.initSign(privkey);

            File infile = new File(args[1]);
            InputStream in = new FileInputStream(infile);
            int length = (int) infile.length();
            byte[] message = new byte[length];
            in.read(message, 0, length);
            in.close();

            signalg.update(message);
            byte[] signature = signalg.sign();

            DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
            int signlength = signature.length;
            out.writeInt(signlength);
            out.write(signature, 0, signlength);
            out.write(message, 0, length);
            out.close();
        } else if (args[0].equals("-verify")) {
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[2]));
            PublicKey pubkey = (PublicKey) keyIn.readObject();
            keyIn.close();

            Signature verifyalg = Signature.getInstance("DSA");
            verifyalg.initVerify(pubkey);

            File infile = new File(args[1]);
            DataInputStream in = new DataInputStream(new FileInputStream(infile));
            int signlength = in.readInt();
            byte[] signature = new byte[signlength];
            in.read(signature, 0, signlength);

            int length = (int) infile.length() - signlength - 4;
            byte[] message = new byte[length];
            in.read(message, 0, length);
            in.close();

            verifyalg.update(message);
            if (!verifyalg.verify(signature))
                System.out.print("not ");
            System.out.println("verified");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:RSATest.java

public static void main(String[] args) {
    try {/*from w  w w .java  2s. c om*/
        if (args[0].equals("-genkey")) {
            KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
            SecureRandom random = new SecureRandom();
            pairgen.initialize(KEYSIZE, random);
            KeyPair keyPair = pairgen.generateKeyPair();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(keyPair.getPublic());
            out.close();
            out = new ObjectOutputStream(new FileOutputStream(args[2]));
            out.writeObject(keyPair.getPrivate());
            out.close();
        } else if (args[0].equals("-encrypt")) {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecureRandom random = new SecureRandom();
            keygen.init(random);
            SecretKey key = keygen.generateKey();

            // wrap with RSA public key
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key publicKey = (Key) keyIn.readObject();
            keyIn.close();

            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.WRAP_MODE, publicKey);
            byte[] wrappedKey = cipher.wrap(key);
            DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
            out.writeInt(wrappedKey.length);
            out.write(wrappedKey);

            InputStream in = new FileInputStream(args[1]);
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            crypt(in, out, cipher);
            in.close();
            out.close();
        } else {
            DataInputStream in = new DataInputStream(new FileInputStream(args[1]));
            int length = in.readInt();
            byte[] wrappedKey = new byte[length];
            in.read(wrappedKey, 0, length);

            // unwrap with RSA private key
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key privateKey = (Key) keyIn.readObject();
            keyIn.close();

            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.UNWRAP_MODE, privateKey);
            Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);

            OutputStream out = new FileOutputStream(args[2]);
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);

            crypt(in, out, cipher);
            in.close();
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

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);//from w w w . j  a v  a2  s. c om
    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:Main.java

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

    Object obj = null;/* ww  w  . j  a v  a  2s  . c om*/

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

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);/* w ww .jav  a2 s  .  c  om*/
    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: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);/*  w  w  w  .  j  a  v a 2 s .  c o  m*/
    oout.writeObject(b);
    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();
}