Note that there are some explanatory texts on larger screens.

plurals
  1. PONewline character omitted while reading from buffer
    primarykey
    data
    text
    <p>I've written the following code:</p> <pre><code>public class WriteToCharBuffer { public static void main(String[] args) { String text = "This is the data to write in buffer!\nThis is the second line\nThis is the third line"; OutputStream buffer = writeToCharBuffer(text); readFromCharBuffer(buffer); } public static OutputStream writeToCharBuffer(String dataToWrite){ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(byteArrayOutputStream)); try { bufferedWriter.write(dataToWrite); bufferedWriter.flush(); } catch (IOException e) { e.printStackTrace(); } return byteArrayOutputStream; } public static void readFromCharBuffer(OutputStream buffer){ ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) buffer; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))); String line = null; StringBuffer sb = new StringBuffer(); try { while ((line = bufferedReader.readLine()) != null) { //System.out.println(line); sb.append(line); } System.out.println(sb); } catch (IOException e) { e.printStackTrace(); } } } </code></pre> <p>When I execute the above code, following is the output:</p> <pre><code>This is the data to write in buffer!This is the second lineThis is the third line </code></pre> <p>Why are the newline characters (\n) skipped? If I uncomment the <strong>System.out.println()</strong> as following:</p> <pre><code>while ((line = bufferedReader.readLine()) != null) { System.out.println(line); sb.append(line); } </code></pre> <p>I get the correct output as:</p> <pre><code>This is the data to write in buffer! This is the second line This is the third line This is the data to write in buffer!This is the second lineThis is the third line </code></pre> <p>What is reason for this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload