Java – BufferedReader when reading JSON data

BufferedReader when reading JSON data… here is a solution to the problem.

BufferedReader when reading JSON data

Why do we add\nwhen reading JSON data with BufferedReader?

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while((line = reader.readLine())!=null){
    sb.append(line + "\n");


You’re not adding \n – you’re putting a newline character after it, which is effectively engulfed by readLine(). For example, if your text file originally consists of 5 lines:


Then reader.readLine() will return (in successive calls) “line1”, “line2”, “line3”, “line4”, “line5" No BufferedReader detected the end of the line at the end of the line

So, if you only have sb.append(line), you’ll end up with a StringBuilder: that contains the following


That being said, this code seems a bit pointless – it really just normalizes line breaks. Unless you really need it, you’re better off using the call read instead of readLine() and copying the text this way….

Related Problems and Solutions