Darano

which way you pref.

Sep 29th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1. BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream());
  2. StringBuilder result = new StringBuilder();
  3.  
  4. //1 way : read char for char from the Inputstream
  5. int charsRead;
  6. char[] buff = new char[500];
  7. while(true){
  8.     charsRead = reader.read(buff);
  9.     if(charsRead < 0)
  10.         break;
  11.     else if(charsRead > 0)
  12.         result.append(String.copyValueOf(buff, 0, charsRead);
  13. }
  14.  
  15.  
  16. //2 way : only using the buff-var in loop scope
  17. for(String line = reader.readLine(); line != null; line = reader.readLine())
  18.     result.append(line).append("\n");
  19.  
  20.  
  21. //3 way : using the buff-var outside the loop scope
  22. String line;
  23.  while(null != (line = reader.readLine()))
  24.     result.append(line).append("\n");
Advertisement
Add Comment
Please, Sign In to add comment