Example usage for java.io DataInputStream readChar

List of usage examples for java.io DataInputStream readChar

Introduction

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

Prototype

public final char readChar() throws IOException 

Source Link

Document

See the general contract of the readChar method of DataInput.

Usage

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);/*ww  w  . j  a  v  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: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);//w  w  w . j a 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: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);

    dos.writeChars(s);//  w  w  w  .  ja va  2 s  . c o 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: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 ww.ja va 2s .c  o  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:MainClass.java

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

        FileInputStream fis = new FileInputStream("fileName.dat");

        // Create a data input stream
        DataInputStream dis = new DataInputStream(fis);

        // Read and display data
        System.out.println(dis.readBoolean());
        System.out.println(dis.readByte());
        System.out.println(dis.readChar());
        System.out.println(dis.readDouble());
        System.out.println(dis.readFloat());
        System.out.println(dis.readInt());
        System.out.println(dis.readLong());
        System.out.println(dis.readShort());

        // Close file input stream
        fis.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

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]);/* w w w .ja v  a2  s . co  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 main(String[] args) throws Exception {
    FileInputStream fin = new FileInputStream("myfile.dat");

    DataInputStream din = new DataInputStream(fin);
    while (true) {
        int theNumber = din.readInt();
        //double theNumber = din.readDouble();
        //System.out.println(din.readUTF());
        //boolean b = din.readBoolean();
        byte b = din.readByte();
        System.out.println("byte : " + b);
        System.out.println(theNumber);

        char ch = din.readChar();
        System.out.println("Char : " + ch);
        float f = din.readFloat();
        System.out.println("float : " + f);
        long l = din.readLong();
        System.out.println("long : " + l);

    }//from  w ww  .j  a  va2  s.  c om
}

From source file:Main.java

public static char readChar(DataInputStream in) throws IOException {
    return in.readChar();
}

From source file:Main.java

public static Object[] readSettings(String file) throws IOException {
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    try {/*from   w ww.  ja v  a 2s. c  o  m*/
        Object[] res = new Object[in.readInt()];
        for (int i = 0; i < res.length; i++) {
            char cl = in.readChar();
            switch (cl) {
            case 'S':
                res[i] = in.readUTF();
                break;
            case 'F':
                res[i] = in.readFloat();
                break;
            case 'D':
                res[i] = in.readDouble();
                break;
            case 'I':
                res[i] = in.readInt();
                break;
            case 'L':
                res[i] = in.readLong();
                break;
            case 'B':
                res[i] = in.readBoolean();
                break;
            case 'Y':
                res[i] = in.readByte();
                break;
            default:
                throw new IllegalStateException("cannot read type " + cl + " from " + file);
            }
        }
        return res;
    } finally {
        in.close();
    }
}

From source file:com.zack6849.alphabot.api.Utils.java

public static String checkServerStatus(InetAddress i, int port) {
    String returns = "Error.";
    try {//w  ww.j  a v  a  2s  .  c  o  m
        //wow...i never actually used the port argument?
        Socket s = new Socket(i, port);
        DataInputStream SS_BF = new DataInputStream(s.getInputStream());
        DataOutputStream d = new DataOutputStream(s.getOutputStream());
        d.write(new byte[] { (byte) 0xFE, (byte) 0x01 });
        SS_BF.readByte();
        short length = SS_BF.readShort();
        StringBuilder sb = new StringBuilder();
        for (int in = 0; in < length; in++) {
            char ch = SS_BF.readChar();
            sb.append(ch);
        }
        String all = sb.toString().trim();
        System.out.println(all);
        String[] args1 = all.split("\u0000");
        if (args1[3].contains("")) {
            returns = "MOTD: " + args1[3].replaceAll("[a-m]", "").replaceAll("[1234567890]", "")
                    + "   players: [" + args1[4] + "/" + args1[5] + "]";
        } else {
            returns = "MOTD: " + args1[3] + "   players: [" + args1[4] + "/" + args1[5] + "]";
        }
    } catch (UnknownHostException e1) {
        returns = "the host you specified is unknown. check your settings.";
    } catch (IOException e1) {
        returns = "sorry, we couldn't reach this server, make sure that the server is up and has query enabled.";
    }
    return returns;
}