BR.readLine()
は最初の行のみを返します。すべての行を読むには、BufferedReader
をループする必要があります。可能な方法の1つは、
String fullMessage;
String aux;
do{
aux = BR.readLine();
fullMessage = fullMessage.concat(aux);
}while(aux != null);
です。これは単なる例です。
ドキュメントを読む:Java用https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()
例7+あなたが使用できるJavaの古いバージョンの
String message = null;
//Try with resources, java will handle the closing of the stream, event if exception is thrown.
try ( InputStreamReader inputStream = new InputStreamReader(socket.getInputStream());
BufferedReader bufferReader = new BufferedReader(inputStream);) {
String aux = null;
do {
aux = bufferReader.readLine();
message = message.concat(aux);
} while (aux != null);
} catch (IOException e) {
System.out.println("Failed to read input stream from socket");
}
System.out.println("Message: " + message);
:
InputStreamReader inputStream = null;
BufferedReader bufferReader = null;
String message = null;
try {
inputStream = new InputStreamReader(socket.getInputStream());
bufferReader = new BufferedReader(inputStream);
String aux = null;
do {
aux = bufferReader.readLine();
message = message.concat(aux);
} while (aux != null);
inputStream.close();
bufferReader.close();
} catch (IOException e) {
//Read throws IOException, don't just use Exception (this could hide other exceptions that you are not treating).
System.out.println("Failed to read input stream from socket");
} finally{
//Use 2 try-catch, if you only use one and the first fails, the second will never close.
try{inputStream.close();}catch(IOException ioe){}
try{bufferReader.close();}catch(IOException ioe){}
}
System.out.println("Message: "+message);
編集:申し訳ありませんがAPPENDはStringBufferをとStringBuilderのためのものですが、私の悪い申し訳ありません。
EDIT2:2つの例を追加しました。
どのJavaのバージョンを使用しますか? –