Newline character omitted while reading from buffer

安稳与你 提交于 2019-12-01 03:39:05

JavaDoc Says

public String readLine()
                throws IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:

From Javadoc

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

You can do something like that

buffer.append(line);
buffer.append(System.getProperty("line.separator"));

Just in case someone wants to read the text with '\n' included.

try this simple approach

So,

Say, You have three lines of data (say in a .txt file) , like this

This is the data to write in buffer!
This is the second line
This is the third line

And while reading, you are doing something like this

    String content=null;
    String str=null;
    while((str=bufferedReader.readLine())!=null){ //assuming you have 
    content.append(str);                     //your bufferedReader declared.
    }
    bufferedReader.close();
    System.out.println(content);

and expecting the output to be

This is the data to write in buffer!
This is the second line
This is the third line

but scratching your head upon seeing output as a single line

This is the data to write in buffer!This is the second lineThis is the third line

Here is what you can do

by adding this piece of code inside your while loop

if(str.trim().length()==0){
   content.append("\n");
}

So now what your while loop should look like

while((str=bufferedReader.readLine())!=null){
    if(str.trim().length()==0){
       content.append("\n");
    }
    content.append(str);
}

Now you get required output (as three lines of text)

This is the data to write in buffer!
This is the second line
This is the third line

This is what the javadocs says for the readLine() method of class BufferedReader

 /**
 * Reads a line of text.  A line is considered to be terminated by any one
 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
 * followed immediately by a linefeed.
 *
 * @return     A String containing the contents of the line, not including
 *             any line-termination characters, or null if the end of the
 *             stream has been reached
 *
 * @exception  IOException  If an I/O error occurs
 */

readline() does not return the platforms line ending. JavaDoc.

This is because of readLine(). From Java Docs:

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

So what is happening is your "\n" are being considered as a line feed so reader considers that to be a line.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!