Example usage for java.io DataOutputStream writeInt

List of usage examples for java.io DataOutputStream writeInt

Introduction

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

Prototype

public final void writeInt(int v) throws IOException 

Source Link

Document

Writes an int to the underlying output stream as four bytes, high byte first.

Usage

From source file:ServerSocketDemo.java

public static void main(String args[]) throws Exception {
    int port = Integer.parseInt(args[0]);

    ServerSocket ss = new ServerSocket(port);

    while (true) {
        Socket s = ss.accept();/*from   w w  w .  ja v  a2s .  com*/

        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeInt(1);

        s.close();
    }
}

From source file:Main.java

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

    int i = 100;/*from   w ww . j  a  v  a  2s .  c  om*/
    dos.writeInt(i);
    dos.close();
}

From source file:Main.java

public static void main(String args[]) {
    try {//from www.j  a va2 s. c o  m

        int port = 5555;
        ServerSocket ss = new ServerSocket(port);

        while (true) {
            // Accept incoming requests
            Socket s = ss.accept();

            // Write result to client
            OutputStream os = s.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            dos.writeInt(100);

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

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);
    dos.writeChar(' ');
    dos.writeUTF("Java Source and Support at www.java2s.com");
    dos.close();/*from   w w  w. j av a2  s  .c o m*/
    //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[] 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);
    dataOut.writeDouble(100.0);/*w  ww  . j av  a 2s  .co  m*/
    dataOut.close();
    buffer.close();
    fileOut.close();
}

From source file:Main.java

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

    int[] i = { 123, 234, 454, 567, 789 };
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    for (int j : i) {
        dos.writeInt(j);
    }//from www . ja  v  a2 s.  c  om
    dos.flush();
    InputStream is = new FileInputStream("c:\\test.txt");

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

From source file:Main.java

public static void main(String[] args) throws IOException {
    int[] i = { 123, 234, 345, 456, 678 };
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    for (int j : i) {
        dos.writeInt(j);
    }//w  ww.  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) {
        int k = dis.readInt();
        System.out.print(k);
    }
}

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  w w  .  ja va  2 s  .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:Main.java

public static void main(String[] arguments) {
    try {//  w  ww  .  ja  v a 2 s  . c  om
        FileOutputStream file = new FileOutputStream("p.dat");
        BufferedOutputStream buff = new BufferedOutputStream(file);
        DataOutputStream data = new DataOutputStream(buff);

        for (int i = 0; i < 400; i++)
            data.writeInt(i);
        data.close();
    } catch (IOException e) {
        System.out.println("Error - " + e.toString());
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    int howMany = 20;
    // To avoid resizing the buffer, calculate the size of the
    // byte array in advance.
    ByteArrayOutputStream bout = new ByteArrayOutputStream(howMany * 4);
    DataOutputStream dout = new DataOutputStream(bout);

    for (int i = 0; i <= 20; i++) {
        dout.writeInt(i);
    }/*from  w w  w  .  j a  v a2  s . co m*/

    FileOutputStream fout = new FileOutputStream("fibonacci.dat");
    try {
        bout.writeTo(fout);
        fout.flush();
    } finally {
        fout.close();
    }
}