2011-01-28 35 views
10

Tôi đã viết đoạn mã sau:Newline nhân vật bỏ qua khi đọc từ bộ đệm

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

} 
} 

Khi tôi thực thi mã trên, sau đây là kết quả:

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

Tại sao các ký tự xuống dòng (\ n) đã bỏ qua? Nếu tôi bỏ ghi chú System.out.println() như sau:

while ((line = bufferedReader.readLine()) != null) { 
     System.out.println(line); 
     sb.append(line); 
     } 

tôi nhận được đầu ra đúng như:

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 

lý do cho điều này là gì?

+0

Bỏ ghi chú 'System.out.println (dòng);' không đưa ra kết quả chính xác, cos 'System.out.println in' một chuỗi với một dòng mới. Hãy thử thay thế nó bằng 'System.out.print (line); ' –

Trả lời

21

JavaDoc Says

public String readLine() 
       throws IOException 

Đọc một dòng văn bản. Một dòng được coi là chấm dứt bởi bất kỳ một trong một dòng feed ('\ n'), một vận chuyển trở lại ('\ r'), hoặc một vận chuyển trở lại theo sau ngay lập tức bởi một linefeed.
Returns:
một chuỗi chứa nội dung của dòng, không bao gồm bất kỳ ký tự dòng chấm dứt, hoặc null nếu sự kết thúc của dòng đã đạt
Ném:

+1

Điều đó giải thích nó. Cảm ơn rất nhiều!!! –

+1

cũng Jigar đánh tôi với nó :) – CoolBeans

+0

+1 để đặt 'trả về' một phần – Nishant

8

Từ Javadoc

Đọc một dòng văn bản. Một dòng được coi là bị chấm dứt bởi bất kỳ một trong một nguồn cấp dữ liệu dòng ('\ n'), trở về vận chuyển ('\ r') hoặc trả về vận chuyển theo sau bởi một dòng cấp.

Bạn có thể làm một cái gì đó như thế

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

+1 thấy câu hỏi này trước tôi :) –

+0

bình luận tốt Jigar :) –

0

readline() không trả lại các nền tảng kết thúc. JavaDoc.

0

Điều này là do readLine(). Từ Java Docs:

Đọc một dòng văn bản. Đường dây là được coi là bị chấm dứt bởi bất kỳ của nguồn cấp dữ liệu dòng ('\ n'), vận chuyển trả lại ('\ r') hoặc trả lại hàng vận chuyển theo sau bởi nguồn cấp dữ liệu dòng.

Vì vậy, điều đang xảy ra là "\ n" của bạn đang được coi là nguồn cấp dữ liệu dòng để người đọc coi đó là một dòng.

1

Đây là những gì javadocs nói cho phương pháp readLine() của lớp BufferedReader

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

Chỉ trong trường hợp ai đó muốn đọc các văn bản với '\n' bao gồm.

thử phương pháp này đơn giản

Vì vậy,

nói, bạn có ba dòng dữ liệu (chẳng hạn trong một file .txt), như

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

trong khi điều này đọc, bạn đang làm điều gì đó như thế này

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

mong đợi đầu ra

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

nhưng gãi đầu của bạn khi nhìn thấy đầu ra như một dòng đơn

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

Đây là những gì bạn có thể làm

bởi thêm đoạn mã này bên trong vòng lặp while

if(str.trim().length()==0){ 
    content.append("\n"); 
} 

bạn Vì vậy, bây giờ những gì while vòng lặp của bạn sẽ trông như thế

while((str=bufferedReader.readLine())!=null){ 
    if(str.trim().length()==0){ 
     content.append("\n"); 
    } 
    content.append(str); 
} 

đầu ra Bây giờ bạn nhận được yêu cầu (như ba dòng văn bản)

This is the data to write in buffer! 
This is the second line 
This is the third line 
Các vấn đề liên quan