Example usage for java.io DataOutputStream writeUTF

List of usage examples for java.io DataOutputStream writeUTF

Introduction

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

Prototype

public final void writeUTF(String str) throws IOException 

Source Link

Document

Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileOutputStream fileOut = new FileOutputStream("data.txt");
    BufferedOutputStream buffer = new BufferedOutputStream(fileOut);
    DataOutputStream dataOut = new DataOutputStream(buffer);
    dataOut.writeUTF("Hello!");
    dataOut.writeInt(4);/*from  w ww.j a  va 2  s  .c  o  m*/
    dataOut.writeDouble(100.0);
    dataOut.close();
    buffer.close();
    fileOut.close();
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
    FileOutputStream fos = new FileOutputStream("out.log");
    DataOutputStream dos = new DataOutputStream(fos);
    while (!queue.isEmpty()) {
        dos.writeUTF(queue.take());
    }/*from w ww  .  j ava 2s  .  c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String serverName = args[0];/*ww  w  . j  a v a2  s .  co  m*/
    int port = Integer.parseInt(args[1]);

    try {
        System.out.println("Connecting to " + serverName + " on port " + port);
        Socket client = new Socket(serverName, port);

        System.out.println("Just connected to " + client.getRemoteSocketAddress());

        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        out.writeUTF("Hello from " + client.getLocalSocketAddress());

        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readUTF());

        client.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String[] s = { "Hello", "tutorial from java2s.com!!" };
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    for (String j : s) {
        dos.writeUTF(j);
    }//from  ww w . j a  v  a  2  s.c o  m
    dos.flush();
    InputStream is = new FileInputStream("c:\\test.txt");
    DataInputStream dis = new DataInputStream(is);
    while (dis.available() > 0) {
        String k = dis.readUTF();
        System.out.print(k);
    }

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String[] s = { "Hello", "from java2s.com!" };
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    for (String j : s) {
        dos.writeUTF(j);
    }/*from   www. j a va2s  .c o  m*/

    dos.flush();

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        String k = dis.readUTF();
        System.out.print(k);
    }
}

From source file:Main.java

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

    String[] s = { "Hello", "java2s.com!" };

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");

    DataOutputStream dos = new DataOutputStream(fos);

    for (String j : s) {
        dos.writeUTF(j);
    }/* w  w w.  j  a  va 2 s . c  om*/

    dos.flush();

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);
    while (dis.available() > 0) {
        String k = dis.readUTF();
        System.out.print(k);
    }

}

From source file:Main.java

public static void main(String[] atgs) throws Exception {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("java.txt")));
    out.writeUTF("");
    out.writeBytes("a");
    out.writeChars("aaa");
    out.close();/*  w  ww  . ja  v  a  2  s  . c om*/
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("java.txt")));
    System.out.println(in.readUTF());
    byte[] buf = new byte[1024];
    int len = in.read(buf);
    System.out.println(new String(buf, 0, len));
    in.close();
}

From source file:InputOutputDemoBinaryFile.java

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

    //Write primitive values to a binary file "java2s.dat":
    DataOutputStream dos = new DataOutputStream(new FileOutputStream("java2s.dat"));
    dos.writeInt(228);//from w w  w .ja v a 2  s  . c o  m
    dos.writeChar(' ');
    dos.writeUTF("Java Source and Support at www.java2s.com");
    dos.close();
    //Read primitive values from binary file "java2s.dat":
    DataInputStream dis = new DataInputStream(new FileInputStream("java2s.dat"));
    System.out.println(dis.readInt() + "|" + dis.readChar() + "|" + dis.readUTF());
}

From source file:paxos.Receiver.java

/**
 * Contoh kode program untuk node yang menerima paket. Idealnya dalam paxos
 * balasan juga dikirim melalui UnreliableSender.
 *//*from   w  w  w  .j a  va2s .  c o m*/
public static void main(String args[]) throws Exception {
    System.out.println("STARTED:");
    int listenPort = 9876;
    /*DatagramSocket serverSocket = new DatagramSocket(listenPort);
            
    byte[] receiveData = new byte[1024];
    while(true)
    {
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            
            String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
            JSONObject receivedMessage = new JSONObject(sentence);
            System.out.println("RECEIVED: " + receivedMessage.optString("method"));
    }*/
    try {

        ServerSocket serverSocket = new ServerSocket(listenPort);
        System.out.println("Server connected\n");

        while (true) {
            Socket server = serverSocket.accept();
            DataInputStream in = new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");

        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("It didn't work");
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
    out2.writeDouble(3.14159);/*from  ww w  .ja  v a  2 s.c  o m*/
    out2.writeUTF("Square root of 2");
    out2.close();

    DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));

    System.out.println(in5.readDouble());
    System.out.println(in5.readUTF());
}