Q:

java, why DataInputStream split String when whitespace

So I tried to learn Socket and DataInputStream.
While I try quite attempt, I realized that DataInputStream read string as divided, means like this:
Client side:
dos = new DataOutputStream(socket.getOutputStream());

String msg = "okay test some String"

dos.writeUTF(msg);

ServerSide :
dis = new DataInputStream(socket.getInputStream());

while(true){

    msg = dis.readUTF();

    Syso(msg);

}

result:
okay

test

some

String

I want to read msg fully in one line. What should I do?

A:

Use the BufferedReader
dis = new BufferedReader(new InputStreamReader(socket.getInputs()))
while(true){
   msg = dis.readLine() 
   System.out.println(msg);
}

Or you can replace System.out.println with System.out.print which does not print a new line character at the end
dis = new DataInputStream(socket.getInputStream());
while(true){
   msg = dis.readUTF();
   System.out.print(msg+" ");
}

