我写了以下代码:
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
这是什么原因?
答案 0 :(得分:22)
public String readLine()
throws IOException
读取一行文字。一条线被认为是由换行符('\ n'),回车符('\ r')或回车后的任何一个终止,后面是换行符。
的返回:强>
包含行内容的String,不包括任何行终止字符;如果已到达流的末尾,则为null 抛出:
答案 1 :(得分:8)
来自Javadoc
阅读一行文字。一行被视为由换行符('\ n'),回车符('\ r')或回车符后面的任何一行终止。
你可以做那样的事情
buffer.append(line);
buffer.append(System.getProperty("line.separator"));
答案 2 :(得分: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
答案 3 :(得分: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
*/
答案 4 :(得分:0)
readline()
不会返回平台行结尾。 JavaDoc
答案 5 :(得分:0)
这是因为readLine()。来自Java Docs:
阅读一行文字。一条线是 被认为被任何人终止 换行符('\ n'),一个马车 返回('\ r')或回车 然后立即换行。
所以正在发生的事情是你的“\ n”被视为换行符,因此读者认为这是一条线。