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:SquareClient.java

public static void main(String args[]) throws Exception {
    String server = args[0];//w w  w.  ja  v  a2  s.  com
    int port = Integer.parseInt(args[1]);
    double value = Double.valueOf(args[2]).doubleValue();

    Socket s = new Socket(server, port);
    OutputStream os = s.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeDouble(value);

    InputStream is = s.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    value = dis.readDouble();

    System.out.println(value);
    s.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://localhost:8080/postedresults.jsp");
    URLConnection conn = url.openConnection();
    conn.setDoInput(true);//from   w  ww  .  ja  v a 2s. c  o m
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    String content = "CONTENT=HELLO JSP !&ONEMORECONTENT =HELLO POST !";

    out.writeBytes(content);
    out.flush();
    out.close();

    DataInputStream in = new DataInputStream(conn.getInputStream());
    String str;
    while (null != ((str = in.readUTF()))) {
        System.out.println(str);
    }
    in.close();
}

From source file:RandomFileTest.java

public static void main(String[] args) {
    Employee[] staff = new Employee[3];

    staff[0] = new Employee("Harry Hacker", 35000);
    staff[1] = new Employee("Carl Cracker", 75000);
    staff[2] = new Employee("Tony Tester", 38000);
    try {/* w ww. j a v  a 2  s . co m*/
        DataOutputStream out = new DataOutputStream(new FileOutputStream("employee.dat"));
        for (int i = 0; i < staff.length; i++)
            staff[i].writeData(out);
        out.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }

    try {
        RandomAccessFile in = new RandomAccessFile("employee.dat", "r");
        int count = (int) (in.length() / Employee.RECORD_SIZE);
        Employee[] newStaff = new Employee[count];

        for (int i = count - 1; i >= 0; i--) {
            newStaff[i] = new Employee();
            in.seek(i * Employee.RECORD_SIZE);
            newStaff[i].readData(in);
        }
        for (int i = 0; i < newStaff.length; i++)
            newStaff[i].print();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }
}

From source file:PrimeWriter.java

public static void main(String[] arguments) {
    int[] primes = new int[400];
    int numPrimes = 0;

    int candidate = 2;
    while (numPrimes < 400) {
        primes[numPrimes] = candidate;/*  w w w  . j ava 2  s  .  c o m*/
        numPrimes++;
        candidate++;
    }

    try {
        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(primes[i]);
        data.close();
    } catch (IOException e) {
        System.out.println("Error - " + e.toString());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String serverName = args[0];//w  ww.  j  av a 2  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:SquareServer.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();//ww w  . java  2 s .  c o  m
        InputStream is = s.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        double value = dis.readDouble();
        value *= value;

        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeDouble(value);

        s.close();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String sessionCookie = null;/*from w  ww.  j av  a2s. c om*/
    URL url = new java.net.URL("http://127.0.0.1/yourServlet");
    URLConnection con = url.openConnection();
    if (sessionCookie != null) {
        con.setRequestProperty("cookie", sessionCookie);
    }
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.flush();
    byte buf[] = byteOut.toByteArray();
    con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close();
    DataInputStream in = new DataInputStream(con.getInputStream());
    int count = in.readInt();
    in.close();
    if (sessionCookie == null) {
        String cookie = con.getHeaderField("set-cookie");
        if (cookie != null) {
            sessionCookie = parseCookie(cookie);
            System.out.println("Setting session ID=" + sessionCookie);
        }
    }

    System.out.println(count);
}

From source file:delete_tcp.java

public static void main(String ar[]) throws IOException {
    ServerSocket ss = new ServerSocket(9999);

    Socket s = ss.accept();/* w  w w. jav a  2  s .  c o  m*/

    DataInputStream in = new DataInputStream(s.getInputStream());
    DataOutputStream out = new DataOutputStream(s.getOutputStream());

    String id = in.readUTF();

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    EmployeeJDBCTemplate employeeJDBCTemplate = (EmployeeJDBCTemplate) context.getBean("employeeJDBCTemplate");

    System.out.println("Deleting Records...");

    employeeJDBCTemplate.delete(Integer.parseInt(id));

    out.writeUTF("Success");

    s.close();

}

From source file:com.ccc.nhr.test1.NhrSocket.java

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

    final String host = "192.168.16.146";
    final int portNumber = 10010;

    System.out.println("Creating socket to '" + host + "' on port " + portNumber);
    Socket socket = new Socket(host, portNumber);

    final NhrDataService nhrConnection = new NhrConnectionBuilder(socket)
            .withInputBufferedReader(new BufferedReader(new InputStreamReader(socket.getInputStream())))
            .withDataInputStream(new DataInputStream(socket.getInputStream()))
            .withDataOutputStream(new DataOutputStream(socket.getOutputStream())).build();

    ReceiverThread receiverThread = new ReceiverThread();
    receiverThread.setNhrConnection(nhrConnection);
    receiverThread.start();//ww w . j  a va2  s .c  o  m

    SenderThread senderThread = new SenderThread();
    senderThread.setNhrConnection(nhrConnection);
    senderThread.start();

}

From source file:RandomFileTest.java

public static void main(String[] args) {
    Employee[] staff = new Employee[3];

    staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
    staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

    try {//from ww w .ja v a  2s .c om
        // save all employee records to the file employee.dat
        DataOutputStream out = new DataOutputStream(new FileOutputStream("employee.dat"));
        for (Employee e : staff)
            e.writeData(out);
        out.close();

        // retrieve all records into a new array
        RandomAccessFile in = new RandomAccessFile("employee.dat", "r");
        // compute the array size
        int n = (int) (in.length() / Employee.RECORD_SIZE);
        Employee[] newStaff = new Employee[n];

        // read employees in reverse order
        for (int i = n - 1; i >= 0; i--) {
            newStaff[i] = new Employee();
            in.seek(i * Employee.RECORD_SIZE);
            newStaff[i].readData(in);
        }
        in.close();

        // print the newly read employee records
        for (Employee e : newStaff)
            System.out.println(e);
    } catch (IOException e) {
        e.printStackTrace();
    }
}