How can I read a String including new line from InputStream object in Java?

Viewed 8713

I have a ServerSocket in Java:

ServerSocket serverSocket = new ServerSocket(1000);

which accepts a clientSocket:

Socket clientSocket;
clientSocket = serverSocket.accept();

Up until now I was reading the input like this:

BufferedReader clientSocketInputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while ((inputLine = clientSocketInputStream.readLine()) != null){
    String message = inputLine;
    // Hack the computer connecting to this one after here

However if the text sent is something like

String stringToBeSent = "Hello\nHowareyou"; 

then I am in trouble. Because I need this text as it is. 2 different Strings do not help me.

How can I read it as it is?

Thanks.

3 Answers
Related