Example usage for java.io DataOutputStream DataOutputStream

List of usage examples for java.io DataOutputStream DataOutputStream

Introduction

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

Prototype

public DataOutputStream(OutputStream out) 

Source Link

Document

Creates a new data output stream to write data to the specified underlying output stream.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    String s = "Hello from java2s.com!";

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    dos.writeBytes(s);//from ww  w .ja  va 2 s  .  co  m
    dos.flush();

    System.out.println(s + " in bytes:");
    for (byte b : baos.toByteArray()) {
        System.out.print(b + ",");
    }
}

From source file:Main.java

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

    byte[] buf = { 87, 64, 72, 31, 90 };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DataOutputStream dos = new DataOutputStream(baos);

    dos.write(buf, 2, 3);/*from   w  ww . ja v a  2  s .  c  o m*/

    dos.flush();

    for (byte b : baos.toByteArray()) {
        System.out.println(b);
    }
}

From source file:Main.java

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

    byte[] buf = { 12, 15, 18, 20, 22, 24 };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DataOutputStream dos = new DataOutputStream(baos);

    for (byte b : buf) {
        dos.writeByte(b);/*from w  w  w  .  ja  va2  s. c  om*/
    }
    dos.flush();

    for (byte b : baos.toByteArray()) {
        System.out.print(b);
    }

}

From source file:Main.java

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

    int[] buf = { 65, 66, 67, 68, 69, 70, 71 };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    for (int i : buf) {
        dos.write(i);/* w  w  w  .  j  a  va  2s  . com*/
    }

    dos.flush();

    for (byte b : baos.toByteArray()) {
        char c = (char) b;
        System.out.print(c);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    MessageDigest md = MessageDigest.getInstance("MD5");
    SomeObject testObject = new SomeObject();

    dos.writeInt(testObject.count);// w  w  w  . ja v a  2  s .co  m
    dos.writeLong(testObject.product);
    dos.writeDouble(testObject.stdDev);
    dos.writeUTF(testObject.name);
    dos.writeChar(testObject.delimiter);
    dos.flush();

    byte[] hashBytes = md.digest(baos.toByteArray());
    BigInteger testObjectHash = new BigInteger(hashBytes);

    System.out.println("Hash " + testObjectHash);

    dos.close();

}

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

From source file:Main.java

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

    boolean[] bools = { true, false, false, true, true, true };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    for (boolean bool : bools) {
        dos.writeBoolean(bool);// w  w  w.j  a v a  2  s .  c  o m
    }

    dos.flush();

    for (byte b : baos.toByteArray()) {
        System.out.print(b);
    }
}

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  w w  . jav a 2  s  .com*/
    dataOut.writeDouble(100.0);
    dataOut.close();
    buffer.close();
    fileOut.close();
}

From source file:DataIODemo.java

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

    FileOutputStream fout = new FileOutputStream("Test.dat");
    DataOutputStream out = new DataOutputStream(fout);

    out.writeDouble(98.6);/*from   w ww .java2s . c  o  m*/
    out.writeInt(1000);
    out.writeBoolean(true);

    out.close();

    FileInputStream fin = new FileInputStream("Test.dat");
    DataInputStream in = new DataInputStream(fin);

    double d = in.readDouble();
    int i = in.readInt();
    boolean b = in.readBoolean();

    System.out.println("Here are the values:  " + d + " " + i + " " + b);

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