Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeObject.

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:CardReader.java

public static void main(String[] args) {
    Card3 card = new Card3(12, Card3.SPADES);
    System.out.println("Card to write is: " + card);

    try {//from   ww w.  j  a  va2  s.c  o  m
        FileOutputStream out = new FileOutputStream("card.out");
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(card);
        oos.flush();
    } catch (Exception e) {
        System.out.println("Problem serializing: " + e);
    }

    Card3 acard = null;

    try {
        FileInputStream in = new FileInputStream("card.out");
        ObjectInputStream ois = new ObjectInputStream(in);
        acard = (Card3) (ois.readObject());
    } catch (Exception e) {
        System.out.println("Problem serializing: " + e);
    }

    System.out.println("Card read is: " + acard);

}

From source file:RSATest.java

public static void main(String[] args) {
    try {//from  ww  w  .  ja  va 2 s  . c o m
        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:MainClass.java

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

    AccountRecordSerializable record;/*from   ww w.  j av a 2s . 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:com.puffywhiteshare.PWSApp.java

/**
 * @param args//from ww  w .ja  v  a  2 s  .c  om
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    CloudBucket cloud = new CloudBucket();
    logger.info("Test my output bitches");
    Yaml y = new Yaml();
    File f = new File("Playing.yml");
    FileInputStream fis = new FileInputStream(f);
    Map m = (Map) y.load(fis);

    Long startFiles = System.currentTimeMillis();
    Iterator<File> fileIterator = FileUtils.iterateFiles(new File("/Users/chad/Pictures/."), null, true);
    do {
        File file = fileIterator.next();
        cloud.add(file);
    } while (fileIterator.hasNext());
    Long endFiles = System.currentTimeMillis();
    System.out.println("Files processing took " + ((endFiles - startFiles)));

    Long startSer = System.currentTimeMillis();
    String filename = "cloud.ser";
    FileOutputStream fos = null;
    ObjectOutputStream out = null;
    try {
        fos = new FileOutputStream(filename);
        out = new ObjectOutputStream(fos);
        out.writeObject(cloud);
        out.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    Long endSer = System.currentTimeMillis();
    System.out.println("Files processing took " + ((endSer - startSer)));

    logger.info(" Size = " + FileUtils.byteCountToDisplaySize(cloud.getSize()));
    logger.info("Count = " + cloud.getCount());

    /*
    Set<String> buckets = m.keySet();
    System.out.println(m.toString());
    for(String bucket : buckets) {
       logger.info("Bucket = " + bucket);
       logger.info("Folders = " + m.get(bucket));
       List<Object> folders = (List<Object>) m.get(bucket);
       for(Object folder : folders) {
    if(folder instanceof String) {
       logger.info("Folder Root = " + folder);
    } else if(folder instanceof Map) {
       logger.info("Folder Map = " + folder);
    }
       }
       BlockingQueue<File> fileFeed = new ArrayBlockingQueue<File>(10);
               
       Map folderMap = (Map) m.get(bucket);
       Set<String> folders = folderMap.entrySet();
       for(String folder : folders) {
    logger.info("Folder = " + folder);
       }
               
    }
    */
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);// w  w w .  j a  va  2  s  .  c  om
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);

    outputStream.writeUnshared(person);

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);//from w ww  .j  a va  2  s  .  co  m
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.writeFields();

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);//w ww. ja  va 2s  .c  om
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    ObjectOutputStream.PutField putField = outputStream.putFields();

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);//w  w  w  .  ja v  a  2  s. c o  m
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.flush();

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);//  w  ww  .ja va  2  s .  c  om
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.reset();

    outputStream.close();
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);//from w  w  w.  ja  va  2 s.  c om
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1);

    outputStream.close();
}