Example usage for java.io ObjectOutputStream close

List of usage examples for java.io ObjectOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the stream.

Usage

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 v  a  2s  .  c om
    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);//from ww  w . j a  v a2 s.  c o m
    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  www.ja  v a 2s. c  o  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);/*www.j av a  2  s  . c  o m*/
    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:LoopingSocketServer.java

public static void main(String args[]) throws Exception {
    ServerSocket servSocket;//from  w ww.j  a  va 2s. 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: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  av  a  2s  .c  o  m*/
    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();
}

From source file:MainClass.java

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

    AccountRecordSerializable record;/*from w  w  w .  ja  va  2  s  .c  om*/

    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:Data.java

public static void main(String[] args) {
    Data data = new Data(1);
    try {//  w  w w. ja va 2s.c o m
        ObjectOutputStream objectOut = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream("C:/test.bin")));
        objectOut.writeObject(data);
        System.out.println("1st Object written has value: " + data.getValue());
        data.setValue(2);
        objectOut.writeObject(data);
        System.out.println("2nd Object written has value: " + data.getValue());
        data.setValue(3);
        objectOut.writeObject(data);
        System.out.println("3rd Object written has value: " + data.getValue());
        objectOut.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    try {
        ObjectInputStream objectIn = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream("C:/test.bin")));
        Data data1 = (Data) objectIn.readObject();
        Data data2 = (Data) objectIn.readObject();
        Data data3 = (Data) objectIn.readObject();
        System.out.println(data1.equals(data2));
        System.out.println(data2.equals(data3));
        System.out.println(data1.getValue());
        System.out.println(data2.getValue());
        System.out.println(data3.getValue());
        objectIn.close();
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

From source file:DataIOTest2.java

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

    // write the data out
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("invoice1.txt"));

    double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
    int[] units = { 12, 8, 13, 29, 50 };
    String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };

    for (int i = 0; i < prices.length; i++) {
        out.writeDouble(prices[i]);// w w  w  .  ja  va  2s .c  om
        out.writeChar('\t');
        out.writeInt(units[i]);
        out.writeChar('\t');
        out.writeChars(descs[i]);
        out.writeChar('\n');
    }
    out.close();

    // read it in again
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("invoice1.txt"));

    double price;
    int unit;
    String desc;
    double total = 0.0;

    try {
        while (true) {
            price = in.readDouble();
            in.readChar(); // throws out the tab
            unit = in.readInt();
            in.readChar(); // throws out the tab
            desc = in.readLine();
            System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price);
            total = total + unit * price;
        }
    } catch (EOFException e) {
    }
    System.out.println("For a TOTAL of: $" + total);
    in.close();
}

From source file:com.icesoft.faces.webapp.parser.TagToComponentMap.java

/**
 * Main method for when this class is run to build the serialized data from
 * a set of TLDS./*from  w w w. j  a v a  2s .  c  o  m*/
 *
 * @param args The runtime arguements.
 */
public static void main(String args[]) {

    /* arg[0] is "new" to create serialzed data or 'old' to read serialized data
       arg[1] is filename for serialized data;
       arg[2...] are tld's to process */

    FileInputStream tldFile = null;

    TagToComponentMap map = new TagToComponentMap();

    if (args[0].equals("new")) {
        // Build new component map from tlds and serialize it;

        for (int i = 2; i < args.length; i++) {
            try {
                tldFile = new FileInputStream(args[i]);
                map.addTagAttrib((InputStream) tldFile);
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }

        try {
            FileOutputStream fos = new FileOutputStream(args[1]);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            oos.flush();
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (args[0].equals("old")) {
        // Build component from serialized data;
        try {
            FileInputStream fis = new FileInputStream(args[1]);
            ObjectInputStream ois = new ObjectInputStream(fis);
            map = (TagToComponentMap) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (args[0].equals("facelets")) {
        // Build new component map from tld, and use that to
        //  generate a Facelets taglib.xml
        // args[0] is command
        // args[1] is output taglib.xml
        // args[2] is input tld

        try {
            FileWriter faceletsTaglibXmlWriter = new FileWriter(args[1]);
            String preamble = "<?xml version=\"1.0\"?>\n"
                    + "<facelet-taglib xmlns=\"http://java.sun.com/xml/ns/javaee\"\n"
                    + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
                    + "xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee "
                    + "http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd\"\n"
                    + "version=\"2.0\">\n";

            String trailer = "</facelet-taglib>\n";
            faceletsTaglibXmlWriter.write(preamble);

            map.setFaceletsTaglibXmlWriter(faceletsTaglibXmlWriter);
            tldFile = new FileInputStream(args[2]);
            map.addTagAttrib((InputStream) tldFile);

            faceletsTaglibXmlWriter.write(trailer);
            faceletsTaglibXmlWriter.flush();
            faceletsTaglibXmlWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }
}