Example usage for java.io ObjectOutputStream flush

List of usage examples for java.io ObjectOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:Main.java

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

    float f = 1.23456f;

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

    oout.writeFloat(f);//from   ww  w.  j a v a  2  s . c  o  m
    oout.writeFloat(1234.56789f);
    oout.flush();
    oout.close();

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

    System.out.println(ois.readFloat());

    System.out.println(ois.readFloat());
    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);

    // write something in the file
    oout.writeBoolean(true);//from  w w  w.  j  a  v a2 s  . 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 a boolean
    System.out.println(ois.readBoolean());
    ois.close();

}

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.writeUnshared(s);//from  w w w  .  j  a v  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.readUnshared());
    ois.close();
}

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  ww  .ja va2 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 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: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();
    oout.close();//from   ww  w .  j a v  a2 s .c  o  m

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

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

    System.out.println(a.isDefault);

    System.out.println(a.string);

    ois.close();
}

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);//  www.  ja v a  2s .  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: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   ww w.  j av  a  2 s .c  o m*/
    oout.flush();

    // create an ObjectInputStream for the file we created before
    Main ois = new Main(new FileInputStream("test.txt"));

    // enable object resolving
    ois.enableResolveObject(true);

    // get the class for string and print the name
    System.out.println(ois.resolveObject(ois.readUTF()));
    ois.close();
}

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  ww w  . j  a  v a  2  s. com*/
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    Main ois = new Main(new FileInputStream("test.txt"));

    // read and print an object and cast it as string
    System.out.println((String) ois.readUTF());

    // get the class for string and print the name
    ObjectStreamClass streamClass = ObjectStreamClass.lookup(Integer.class);
    System.out.println(ois.resolveClass(streamClass).getName());
    ois.close();
}

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);

    oout.writeUTF(s);//from   www .  ja v  a 2s. c  om
    oout.writeUTF("This is an example from java2s.com");
    oout.flush();
    oout.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    System.out.println(ois.readUTF());

    System.out.println(ois.readUTF());
    ois.close();
}

From source file:Main.java

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

    String s = "Hello World!";
    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    oout.writeUTF(s);//from  ww  w .ja v  a 2  s  . c  o m
    oout.writeUTF("This is an example from java2s.com");
    oout.flush();
    oout.close();

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

    ois.skipBytes(4);
    for (int i = 0; i < ois.available() - 4; i++) {
        System.out.print((char) ois.readByte());
    }
    ois.close();
}