java - Reading from InputStream and appending to String -


i tried 2 difference approaches read bytes inputstream , append contents string/print string:

1.)

buffer= new byte[32768];    while((read= is.read(buffer))>0){           system.out.println(new string(buffer));     } 

2.)

     bufferedreader br = null;      stringbuilder sb = new stringbuilder();      string line;      br = new bufferedreader(new inputstreamreader(is));         while ((line = br.readline()) != null) {                sb.append(line);         }       br.close(); 

while second approach works, first approach gets first few bytes , stops.

could please tell me what's wrong approach 1?

approach 1 wrong because not take account ther real number of bytes is.read(buffer) reads buffer

approach 2 wrong because discards end of line characters.

consider java.nio.file.files.readallbytes or readalllines


Comments