从缓冲区读取时省略了换行符

我写了以下代码:

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 

这是什么原因?

JavaDoc说

 public String readLine() throws IOException 

读一行文字。 一条线被认为是由换行符(’\ n’),回车符(’\ r’)或回车符中的任何一个终止,后面紧跟换行符。
返回:
包含行内容的String,不包括任何行终止字符;如果已到达流的末尾,则为null
抛出:

来自Javadoc

阅读一行文字。 一条线被认为是由换行符(’\ n’) ,回车符(’\ r’)或回车符中的任何一个终止,后面紧跟换行符。

你可以做那样的事情

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

以防万一有人想要阅读包含'\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); } 

现在您获得所需的输出(作为三行文本)

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

这就是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 */ 

readline()不返回平台行结尾。 JavaDoc 。

这是因为readLine()。 来自Java Docs :

阅读一行文字。 一条线被认为是由换行符(’\ n’),回车符(’\ r’)或回车符中的任何一个终止,后面紧跟换行符。

所以正在发生的事情是你的“\ n”被视为换行符,因此读者认为这是一条线。