Example usage for java.io DataOutputStream writeChar

List of usage examples for java.io DataOutputStream writeChar

Introduction

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

Prototype

public final void writeChar(int v) throws IOException 

Source Link

Document

Writes a char to the underlying output stream as a 2-byte value, high byte first.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/WriteChar.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    int ch = 65;/*w  w  w.j ava  2 s.c o m*/
    dos.writeChar(ch);
    dos.close();
}

From source file:Main.java

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

    byte[] buf = { 65, 66, 67, 68, 69, 70 };
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    for (byte b : buf) {
        dos.writeChar(b);
    }// w  w  w  .j  av  a2 s  .  c om
    dos.flush();
    InputStream is = new FileInputStream("c:\\test.txt");
    DataInputStream dis = new DataInputStream(is);
    while (dis.available() > 0) {
        char c = dis.readChar();
        System.out.print(c);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String os = "abc";
    for (byte b : os.getBytes("UTF-16BE")) {
        System.out.println(b);/*from ww w. ja  va2  s.c  om*/
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    for (char c : os.toCharArray()) {
        dos.writeChar(c);
    }

    byte[] ba = baos.toByteArray();

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

From source file:Main.java

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

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

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

    DataOutputStream dos = new DataOutputStream(fos);

    for (byte b : buf) {
        dos.writeChar(b);
    }//from   w  w w .  j  ava2s.  co  m
    dos.flush();

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

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        char c = dis.readChar();
        System.out.print(c);
    }

}

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 .j a  v  a 2  s .co  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:MainClass.java

public static void main(String args[]) {
    try {/*from w  w w  .  j  a v a  2  s .c  o  m*/

        FileOutputStream fos = new FileOutputStream(args[0]);

        DataOutputStream dos = new DataOutputStream(fos);

        dos.writeBoolean(false);
        dos.writeByte(Byte.MAX_VALUE);
        dos.writeChar('A');
        dos.writeDouble(Double.MAX_VALUE);
        dos.writeFloat(Float.MAX_VALUE);
        dos.writeInt(Integer.MAX_VALUE);
        dos.writeLong(Long.MAX_VALUE);
        dos.writeShort(Short.MAX_VALUE);

        fos.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

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);// ww  w.  j  a va  2 s. com
    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:DataIODemo.java

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

    // write the data out
    DataOutputStream out = new DataOutputStream(new FileOutputStream("invoice1.txt"));

    double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
    int[] units = { 12, 8, 13, 29, 50 };
    String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };

    for (int i = 0; i < prices.length; i++) {
        out.writeDouble(prices[i]);/*from   w ww.j a v a  2s .  c  o m*/
        out.writeChar('\t');
        out.writeInt(units[i]);
        out.writeChar('\t');
        out.writeChars(descs[i]);
        out.writeChar('\n');
    }
    out.close();

    // read it in again
    DataInputStream in = new DataInputStream(new FileInputStream("invoice1.txt"));

    double price;
    int unit;
    StringBuffer desc;
    double total = 0.0;

    try {
        while (true) {
            price = in.readDouble();
            in.readChar(); // throws out the tab
            unit = in.readInt();
            in.readChar(); // throws out the tab
            char chr;
            desc = new StringBuffer(20);
            char lineSep = System.getProperty("line.separator").charAt(0);
            while ((chr = in.readChar()) != lineSep)
                desc.append(chr);
            System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price);
            total = total + unit * price;
        }
    } catch (EOFException e) {
    }
    System.out.println("For a TOTAL of: $" + total);
    in.close();
}

From source file:Main.java

public static void writeChar(DataOutputStream out, char val) throws IOException {
    out.writeChar(val);
}

From source file:net.datenwerke.transloader.primitive.WrapperConverter.java

private static void write(Object wrapper, DataOutputStream dataStream) throws IOException {
    char typeCode = getTypeCode(wrapper);
    switch (typeCode) {
    case 'B':
        dataStream.writeByte(intValue(wrapper));
        break;/*from  ww w. jav a  2s. c om*/
    case 'C':
        dataStream.writeChar(charValue(wrapper));
        break;
    case 'S':
        dataStream.writeShort(intValue(wrapper));
        break;
    case 'I':
        dataStream.writeInt(intValue(wrapper));
        break;
    case 'J':
        dataStream.writeLong(number(wrapper).longValue());
        break;
    case 'F':
        dataStream.writeFloat(number(wrapper).floatValue());
        break;
    case 'D':
        dataStream.writeDouble(number(wrapper).doubleValue());
        break;
    case 'Z':
        dataStream.writeBoolean(booleanValue(wrapper));
    }
}