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 {

    long l = 12345678909876L;

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

    // write something in the file
    oout.writeLong(l);/*from  w  w  w  .  j  ava  2  s  .  c o  m*/
    oout.writeLong(987654321L);
    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 long
    System.out.println(ois.readLong());

    // read and print a long
    System.out.println(ois.readLong());
    ois.close();
}

From source file:AESTest.java

public static void main(String[] args) {
    try {//from   www. j a v  a2 s. c om
        if (args[0].equals("-genkey")) {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecureRandom random = new SecureRandom();
            keygen.init(random);
            SecretKey key = keygen.generateKey();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(key);
            out.close();
        } else {
            int mode;
            if (args[0].equals("-encrypt"))
                mode = Cipher.ENCRYPT_MODE;
            else
                mode = Cipher.DECRYPT_MODE;

            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key key = (Key) keyIn.readObject();
            keyIn.close();

            InputStream in = new FileInputStream(args[1]);
            OutputStream out = new FileOutputStream(args[2]);
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(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:edu.msu.cme.rdp.graph.utils.BloomFilterStats.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("USAGE: BloomFilterStats <bloom_filter>");
        System.exit(1);/*from  w w w  .  ja  va  2 s .c  om*/
    }

    File bloomFile = new File(args[0]);

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

    printStats(filter, System.out);
}

From source file:Main.java

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

    short s = 56;

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

    // write something in the file
    oout.writeShort(s);/*  w  w  w.java 2  s. c om*/
    oout.writeShort(new Short("1"));
    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 short
    System.out.println(ois.readShort());

    // read and print a short
    System.out.println(ois.readShort());
    ois.close();
}

From source file:Main.java

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

    double b = 123.234d;

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

    // write something in the file
    oout.writeDouble(b);//from   w w w  . j a  v a  2 s. c  o  m
    oout.writeDouble(456.789d);
    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 double
    System.out.println(ois.readDouble());

    // read and print a double
    System.out.println(ois.readDouble());
    ois.close();
}

From source file:Main.java

public static void main(String[] args) {
    int[] x = new int[] { 1, 2, 3 };
    int[] y = new int[] { 4, 5, 6 };
    Polygon polygon = new Polygon(x, y, x.length);
    try {/*from   w  ww .  j a  va2s.c  o  m*/
        ObjectOutputStream objectOut = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream("Polygons.bin")));
        objectOut.writeObject(polygon);
        objectOut.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    try {
        ObjectInputStream objectIn = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream("Polygons.bin")));
        Polygon theLine = (Polygon) objectIn.readObject();
        System.out.println(theLine);
        objectIn.close();
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

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

    byte b = 12;//from   ww  w  .  ja v  a 2 s.c  om

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

    // write something in the file
    oout.writeByte(b);
    oout.writeByte(21);
    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 byte
    System.out.println((char) ois.readByte());

    // read and print a byte
    System.out.println((char) ois.readByte());
    ois.close();

}

From source file:TestCipher.java

public static void main(String args[]) throws Exception {
    Set set = new HashSet();
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        Point point = new Point(random.nextInt(1000), random.nextInt(2000));
        set.add(point);/*from   w  w  w  .j ava2s  . c  om*/
    }
    int last = random.nextInt(5000);

    // Create Key
    byte key[] = password.getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

    // Create Cipher
    Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    // Create stream
    FileOutputStream fos = new FileOutputStream("out.des");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    CipherOutputStream cos = new CipherOutputStream(bos, desCipher);
    ObjectOutputStream oos = new ObjectOutputStream(cos);

    // Write objects
    oos.writeObject(set);
    oos.writeInt(last);
    oos.flush();
    oos.close();

    // Change cipher mode
    desCipher.init(Cipher.DECRYPT_MODE, secretKey);

    // Create stream
    FileInputStream fis = new FileInputStream("out.des");
    BufferedInputStream bis = new BufferedInputStream(fis);
    CipherInputStream cis = new CipherInputStream(bis, desCipher);
    ObjectInputStream ois = new ObjectInputStream(cis);

    // Read objects
    Set set2 = (Set) ois.readObject();
    int last2 = ois.readInt();
    ois.close();

    // Compare original with what was read back
    int count = 0;
    if (set.equals(set2)) {
        System.out.println("Set1: " + set);
        System.out.println("Set2: " + set2);
        System.out.println("Sets are okay.");
        count++;
    }
    if (last == last2) {
        System.out.println("int1: " + last);
        System.out.println("int2: " + last2);
        System.out.println("ints are okay.");
        count++;
    }
    if (count != 2) {
        System.out.println("Problem during encryption/decryption");
    }
}

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);/*  w w  w  . j a  v  a2s  .  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 whole content
    byte[] b = new byte[13];
    ois.readFully(b);
    String array = new String(b);
    System.out.println(array);
    ois.close();
}

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

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("USAGE: BloomFilterStats <bloom_filter>");
        System.exit(1);// w  ww  .ja v a 2  s .co  m
    }

    File bloomFile = new File(args[0]);

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

    printStats(filter, System.out);

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String line;
    CodonWalker walker = null;
    while ((line = reader.readLine()) != null) {
        char[] kmer = line.toCharArray();
        System.out.print(line + "\t");
        try {
            walker = filter.new RightCodonFacade(kmer);
            walker.jumpTo(kmer);
            System.out.print("present");
        } catch (Exception e) {
            System.out.print("not present\t" + e.getMessage());
        }
        System.out.println();
    }
}