从缓冲区读取时省略换行符
Posted
技术标签:
【中文标题】从缓冲区读取时省略换行符【英文标题】:Newline character omitted while reading from buffer 【发布时间】:2011-06-17 01:14:09 【问题描述】:我写了以下代码:
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
这是什么原因?
【问题讨论】:
取消注释System.out.println(line);
不会给出正确的输出,因为 System.out.println prints
是带有换行符的字符串。尝试将其替换为System.out.print(line);
【参考方案1】:
以防万一有人想阅读包含'\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
【讨论】:
【参考方案2】:这就是 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
*/
【讨论】:
【参考方案3】:来自Javadoc
读取一行文本。一行被视为以换行符 ('\n')、回车符 ('\r') 或回车符后紧跟换行符中的任何一种来终止。 p>
你可以这样做
buffer.append(line);
buffer.append(System.getProperty("line.separator"));
【讨论】:
【参考方案4】:这是因为 readLine()。来自Java Docs:
读取一行文本。一条线是 被任何人视为终止 换行符('\n'),回车 return ('\r'),或回车 紧接着是换行符。
所以发生的事情是您的“\n”被视为换行符,因此读者认为这是一行。
【讨论】:
【参考方案5】:readline()
不返回平台行结尾。 JavaDoc.
【讨论】:
【参考方案6】:JavaDoc 说
public String readLine()
throws IOException
读取一行文本。行被视为由换行符 ('\n')、回车符 ('\r') 或回车符后紧跟换行符中的任何一个终止。返回: 包含行内容的字符串,不包括任何行终止字符,如果已到达流的末尾,则为 null 抛出:
【讨论】:
@jigar,你知道有哪位阅读器可以读取换行符吗? @Elliot 你可以逐字符阅读read()
以上是关于从缓冲区读取时省略换行符的主要内容,如果未能解决你的问题,请参考以下文章
从 ipc fifo 文件描述符读取缓冲区时未初始化 QDataStream