11

我编写了以下代码:

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

这是什么原因?

4

6 回答 6

22

JavaDoc

public String readLine()
                throws IOException

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

于 2011-01-28T05:53:24.927 回答
8

来自Javadoc

阅读一行文字。一行被认为是由换行符('\n')、回车符 ('\r') 或紧跟换行符的回车符中的任何一个终止的。

你可以做这样的事情

buffer.append(line);
buffer.append(System.getProperty("line.separator"));
于 2011-01-28T05:52:59.490 回答
3

以防万一有人想阅读'\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
于 2017-04-26T12:47:36.643 回答
1

这就是 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
 */
于 2011-01-28T06:09:55.673 回答
0

readline()不返回平台行结尾。Java 文档

于 2011-01-28T05:53:36.230 回答
0

这是因为 readLine()。来自Java 文档

阅读一行文字。一行被认为是由换行符 ('\n')、回车符 ('\r') 或紧跟换行符的回车符中的任何一个终止的。

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

于 2011-01-28T05:54:50.957 回答