Java 从缓冲区读取时忽略换行符

Java 从缓冲区读取时忽略换行符,java,bufferedreader,linefeed,Java,Bufferedreader,Linefeed,我编写了以下代码: 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); readFrom

我编写了以下代码:

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();
  }

 }
}
当我执行上述代码时,以下是输出:

This is the data to write in buffer!This is the second lineThis is the third line
为什么要跳过换行符(\n)?如果我取消注释System.out.println(),如下所示:

while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
        sb.append(line);
       }
我得到的正确输出如下:

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
原因是什么?

来自

读一行文字。换行符('\n')、回车符('\r')或紧接着换行符的回车符中的任何一个都会将行视为终止

你可以这样做

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

读一行文字。一行被认为是由换行符('\n')、回车符('\r')或回车符紧跟着换行符中的任何一个终止的。
返回:
包含行内容的字符串,不包括任何行终止字符,如果已到达流的结尾,则为null
抛出:


readline()
不返回平台行结尾

这是因为readLine()。发件人:

读一行文字。一行是 视为被任何一方终止 对于换行符('\n'),一个回车符 返回('\r'),或回车符 紧接着是换行符


现在发生的事情是,您的“\n”被视为一个换行符,所以读者认为这是一个换行符。

这是javadocs对BufferedReader类的readLine()方法所说的

 /**
 * 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
 */

以防有人想阅读包含
'\n'
的文本

试试这个简单的方法

所以

比如说,您有三行数据(比如在
.txt
文件中),如下所示

This is the data to write in buffer!
This is the second line
This is the third line
当你阅读的时候,你正在做这样的事情

    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);
期望输出

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
以下是您可以做的

通过在while循环中添加这段代码

if(str.trim().length()==0){
   content.append("\n");
}
那么现在您的
while
循环应该是什么样子呢

while((str=bufferedReader.readLine())!=null){
    if(str.trim().length()==0){
       content.append("\n");
    }
    content.append(str);
}
现在您获得了所需的输出(作为三行文本)


取消注释
System.out.println(行)
没有给出正确的输出,cos
System.out.println打印带有换行符的字符串。尝试将其替换为
系统输出打印(行)@jigar,你知道有哪位读者可以阅读新行字符的行吗?@Elliot你可以逐字符阅读
while((str=bufferedReader.readLine())!=null){
    if(str.trim().length()==0){
       content.append("\n");
    }
    content.append(str);
}
This is the data to write in buffer!
This is the second line
This is the third line