2011-01-28 18 views
10

से पढ़ते समय मैं निम्नलिखित कोड लिखा है छोड़े गए:newline वर्ण बफर

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

न्यू लाइन पात्रों क्यों कर रहे हैं (:

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

} 
} 

जब मैं ऊपर कोड निष्पादित, निम्न उत्पादन होता है \ n) छोड़ दिया?

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 ((line = bufferedReader.readLine()) != null) { 
     System.out.println(line); 
     sb.append(line); 
     } 

मैं के रूप में सही आउटपुट प्राप्त: यदि मैं निम्नलिखित के रूप में println() uncomment?

+0

(पाठ की तीन लाइनों के रूप में) 'System.out.println (लाइन) को अनमोलमेंट करना;' सही आउटपुट नहीं देता है, 'system.out.println प्रिंट्स' एक स्ट्रिंग को एक नई लाइन के साथ नहीं देता है। इसे 'System.out.print (line) के साथ बदलने का प्रयास करें; ' –

उत्तर

21

JavaDoc की तरह कुछ कर सकते हैं कहते हैं

public String readLine() 
       throws IOException 

टेक्स्ट की पंक्ति में पढ़ता है। लाइन लाइन ('\ n'), एक कैरिज रिटर्न ('\ r'), या एक कैरिज रिटर्न तुरंत लाइनफेड द्वारा पीछा किया जाता है।
रिटर्न:
एक स्ट्रिंग लाइन की सामग्री से युक्त, किसी भी लाइन समाप्ति अक्षरों का समावेश नहीं है, या अशक्त यदि धारा के अंत
तक पहुँच गया है फेंकता:

+1

यह समझाता है। बहुत बहुत धन्यवाद!!! –

+1

अच्छी तरह से जिगर ने मुझे इसे हराया :) – CoolBeans

+0

+1 'रिटर्न' भाग – Nishant

8

से Javadoc

टेक्स्ट की पंक्ति में पढ़ें। लाइन लाइन ('\ n'), एक कैरिज रिटर्न ('\ r'), या एक कैरिज रिटर्न तुरंत लाइनफ़ेड द्वारा पीछा किया जाता है, किसी भी पंक्ति को समाप्त किया जाता है।

आपको लगता है कि

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

+1 ने मेरे सामने यह प्रश्न देखा :) –

+0

अच्छी टिप्पणी जिगार :) –

0

readline() नहीं है वापसी प्लेटफॉर्म लाइन समाप्त हो रही है। JavaDoc

0

यह readLine() की वजह से है। Java Docs से:

टेक्स्ट की एक पंक्ति पढ़ें। एक लाइन को लाइन फीड ('\ n') के किसी भी , एक गाड़ी रिटर्न ('\ r'), या एक कैरिज रिटर्न द्वारा तुरंत लाइनफ़ेड द्वारा समाप्त किया जाने वाला माना जाता है।

तो क्या हो रहा है कि आपके "\ n" को लाइन फीड के रूप में माना जा रहा है, इसलिए पाठक मानता है कि यह एक रेखा है।

1

यह वही है javadocs वर्ग 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

शायद ज़रुरत पड़े किसी '\n' शामिल साथ पाठ पढ़ने के लिए चाहता है की विधि ReadLine() के लिए कहते हैं।

इस सरल तरीके का प्रयास करें

तो,

कहते हैं, आप (एक .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 

यहाँ के रूप में उत्पादन देख कर अपने सिर खरोंच आप क्या कर सकते है

द्वारा तो अब आपके जबकि पाश

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 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^