Example usage for java.io ObjectOutputStream writeUTF

List of usage examples for java.io ObjectOutputStream writeUTF

Introduction

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

Prototype

public void writeUTF(String str) throws IOException 

Source Link

Document

Primitive data write of this String in modified UTF-8 format.

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.writeUTF("Hello World from java2s.com");
    oout.flush();/*  w  ww. j a  v  a 2 s . com*/
    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: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);
    oout.writeUTF("This is an example from java2s.com");
    oout.flush();//from ww  w  . j  av  a  2 s.c om
    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 {

    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 w w.j av  a  2  s. co  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: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);
    oout.writeUTF("This is an example from java2s.com");
    oout.flush();// ww  w . j  a va 2  s.  c o  m
    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();
}

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);
    oout.flush();/*from  w ww . j ava 2  s.co m*/
    oout.close();

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

    String[] list = { Serializable.class.getName() };

    System.out.println(ois.resolveProxyClass(list));
    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.writeUTF("Hello World from java2s.com");
    oout.flush();// w ww .  j  a  v a 2  s .c o  m
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // check how many bytes are available
    System.out.println(ois.available());
    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.writeUTF("Hello World from java2s.com");
    oout.flush();/*  w  w  w.j a v  a  2s . com*/

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

    // read from the stream
    for (int i = 0; i < ois.available();) {
        System.out.print((char) ois.read());
    }

    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);
    oout.flush();/*from  w w w.  java2 s . c o  m*/
    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 {

    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);
    oout.flush();/*from  w  w  w  .j a  v  a2s.c o  m*/
    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);
    oout.flush();/*ww  w  .ja va  2  s . com*/

    // 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();
}