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: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();//w w  w .  ja v a  2  s .com
    oout.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    Example a = (Example) ois.readObject();

    System.out.println(a.s);
    ois.close();
}

From source file:ObjServer.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Hashtable hash = new Hashtable();
    hash.put("Java Source and Support", "www.java2s.com");

    while (true) {
        System.out.println("Listening");
        Socket sock = ssock.accept();

        ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream());
        ostream.writeObject(hash);//from w w  w. j av  a  2 s. c  o  m
        ostream.close();
        sock.close();
    }
}

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.writeUTF("Hello World from java2s.com");
    oout.flush();//from  w w w .  java2  s.  co m
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    for (int i = 0; i < ois.available();) {
        System.out.print((char) ois.read());
    }
    ois.close();
}

From source file:SimpleSocketServer.java

  public static void main(String args[]) throws Exception {
  ServerSocket serverSocket;//from ww w.j a  v  a2  s .  co m
  int portNumber = 1777;
  Socket socket;
  String str;

  str = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  str += "<ticketRequest><customer custID=\"1\">";
  str += "</ticketRequest>";

  serverSocket = new ServerSocket(portNumber);

  System.out.println("Waiting for a connection on " + portNumber);

  socket = serverSocket.accept();

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

  oos.writeObject(str);

  oos.close();

  socket.close();

}

From source file:Main.java

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

    byte[] cbuf = new byte[10];

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

    oout.writeUTF("Hello World from java2s.com");
    oout.flush();/*from   w  ww.  j ava 2s.  c  o m*/
    oout.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    ois.read(cbuf, 0, 7);

    for (int i = 0; i < 7; i++) {
        System.out.print((char) cbuf[i]);
    }
    ois.close();
}

From source file:InputOutputDemoObjectBinaryFile.java

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

    //Write an object or array to binary file "java2sObject.dat":
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("java2sObject.dat"));
    oos.writeObject(new int[] { 2, 3, 5, 7, 11 });
    oos.close();
    //Read objects or arrays from binary file "o.dat":
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("java2sObject.dat"));
    int[] ia = (int[]) (ois.readObject());
    System.out.println(ia[0] + "," + ia[1] + "," + ia[2] + "," + ia[3] + "," + ia[4]);

}

From source file:Main.java

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

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));
    out.write(1);/*from   w  w  w  .j a  va2  s .co  m*/
    out.close();

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    byte[] byteArray = new byte[10];
    in.read(byteArray);
    System.out.println(Arrays.toString(byteArray));
    in.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    short b = 32767;

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

    // write something in the file
    oout.writeShort(b);//from  www . j a v  a 2  s  .  com
    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 short
    System.out.println(ois.readUnsignedShort());
    ois.close();
}

From source file:Main.java

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

    byte b = 127;

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

    // write something in the file
    oout.writeByte(b);/* w  w w . j av  a2  s  .c  om*/
    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 unshared object
    System.out.println(ois.readUnsignedByte());
    ois.close();
}

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();/*  w w  w. j  a v  a  2 s.c om*/
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    Example a = (Example) ois.readObject();

    System.out.println(a.s);

    a.validateObject();
    ois.close();
}