關於java中BufferedReader的read()及readLine()方法的使用心得

BufferedReader的readLine()方法是阻塞式的, 若是到達流末尾, 就返回null, 但若是client的socket末經關閉就銷燬, 則會產生IO異常. 正常的方法就是使用socket.close()關閉不須要的socket.java

 

從一個有若干行的文件中依次讀取各行,處理後輸出,若是用如下方法,則會出現除第一行外行首字符丟失現象socket

String str  = null;
br=new BufferedReader(new FileReader(fileName));
do{
  str = buf.readLine()); 
}while(br.read()!=-1);
如下用法會使每行都少首字符
while(br.read() != -1){
       str = br.readLine();     
 }
緣由就在於br.read() != -1 這判斷條件上。 由於在執行這個條件的時候其實它已經讀取了一個字符了,然而在這裏並無對讀取出來的這個字符作處理,因此會出現少一個字符,若是你這裏寫的是while(br.readLine()!=null)會出現隔一行少一行!

建議使用如下方法
String str = null;
      while((str = br.readLine()) != null){
      //System.out.println(str);//此時str就保存了一行字符串
}函數

這樣應該就能夠無字符丟失地獲得一行了oop

 

 

雖然寫IO方面的程序很少,但BufferedReader/BufferedInputStream卻是用過好幾回的,緣由是:學習

  • 它有一個很特別的方法:readLine(),使用起來特別方便,每次讀回來的都是一行,省了不少手動拼接buffer的瑣碎;
  • 它比較高效,相對於一個字符/字節地讀取、轉換、返回來講,它有一個緩衝區,讀滿緩衝區才返回;通常狀況下,都建議使用它們把其它Reader/InputStream包起來,使得讀取數據更高效。
  • 對於文件來講,常常遇到一行一行的,特別相符情景。

此次是在藍牙開發時,使用兩個藍牙互相傳數據(即一個發一個收),bluecove這個開源組件已經把數據讀取都封裝成InputStream了,也就至關於平時的IO讀取了,很天然就使用起readLine()來了。spa

 

發數據:.net

 

[java] view plaincopy
 
 
  1. BufferedWriter output = new BufferedWriter(new OutputStreamWriter(conn.openOutputStream()));   
  2. int i = 1;  
  3. String message = "message " + i;  
  4. while(isRunning) {  
  5.     output.write(message+"/n");   
  6.     i++;  
  7. }  

 

 

 

讀數據:orm

 

[java]  view plain copy
 
 
  1. BufferedReader input = new BufferedReader(new  InputStreamReader(m_conn.openInputStream()));  
  2. String message = "";  
  3. String line = null;  
  4. while((line = m_input.readLine()) != null) {  
  5.     message += line;  
  6. }  
  7. System.out.println(message);  

 

 

 

上面是代碼的節選,使用這段代碼會發現寫數據時每次都成功,而讀數據側卻一直沒有數據輸出(除非把流關掉)。通過折騰,原來這裏面有幾個大問題須要理解:blog

  • 誤覺得readLine()是讀取到沒有數據時就返回null(由於其它read方法當讀到沒有數據時返回-1),而實際上readLine()是一個阻塞函數,當沒有數據讀取時,就一直會阻塞在那,而不是返回null;由於readLine()阻塞後,System.out.println(message)這句根本就不會執行到,因此在接收端就不會有東西輸出。要想執行到System.out.println(message),一個辦法是發送完數據後就關掉流,這樣readLine()結束阻塞狀態,而可以獲得正確的結果,但顯然不能傳一行就關一次數據流;另一個辦法是把System.out.println(message)放到while循環體內就能夠。
  • readLine()只有在數據流發生異常或者另外一端被close()掉時,纔會返回null值。
  • 若是不指定buffer大小,則readLine()使用的buffer有8192個字符。在達到buffer大小以前,只有遇到"/r"、"/n"、"/r/n"纔會返回。

readLine()的實質(下面是從JDK源碼摘出來的):ip

 

[java] view plaincopy
 
 
  1. String readLine(boolean ignoreLF) throws IOException {  
  2.     StringBuffer s = null;  
  3.     int startChar;  
  4.         synchronized (lock) {  
  5.             ensureOpen();  
  6.         boolean omitLF = ignoreLF || skipLF;  
  7.         bufferLoop:  
  8.         for (;;) {  
  9.         if (nextChar >= nChars)  
  10.             fill(); //在此讀數據  
  11.         if (nextChar >= nChars) { /* EOF */  
  12.             if (s != null && s.length() > 0)  
  13.             return s.toString();  
  14.             else  
  15.             return null;  
  16.         }  
  17.       ......//其它  
  18. }  
  19.   
  20. private void fill() throws IOException {  
  21.     ..../其它  
  22.     int n;  
  23.     do {  
  24.         n = in.read(cb, dst, cb.length - dst); //實質  
  25.     } while (n == 0);  
  26.     if (n > 0) {  
  27.         nChars = dst + n;  
  28.         nextChar = dst;  
  29.     }  
  30.     }  

 

從上面看出,readLine()是調用了read(char[] cbuf, int off, int len) 來讀取數據,後面再根據"/r"或"/n"來進行數據處理。

 

在Java I/O書上也說了:

public String readLine() throws IOException
This method returns a string that contains a line of text from a text file. /r, /n, and /r/n are assumed to be line breaks and are not included in the returned string. This method is often used when reading user input from System.in, since most platforms only send the user's input to the running program after the user has typed a full line (that is, hit the Return key).
readLine() has the same problem with line ends that DataInputStream's readLine() method has; that is, the potential to hang on a lone carriage return that ends the stream . This problem is especially acute on networked connections, where readLine() should never be used.

 

小結,使用readLine()必定要注意:

  1. 讀入的數據要注意有/r或/n或/r/n
  2. 沒有數據時會阻塞,在數據流異常或斷開時纔會返回null
  3. 使用socket之類的數據流時,要避免使用readLine(),以避免爲了等待一個換行/回車符而一直阻塞

 

 

 

之前學習的時候也沒有太在乎,在項目中使用到了才發現呵呵

1.讀取一個txt文件,方法不少種我使用了字符流來讀取(爲了方便)

 

  FileReader fr = new FileReader("f:\\TestJava.Java");
   BufferedReader bf = new BufferedReader(fr);

//這裏進行讀取

int b;
   while((b=bf.read())!=-1){
    System.out.println(bf.readLine());
   }

發現每行的第一個字符都沒有顯示出來,緣由呢:b=bf.read())!=-1  每次都會先讀取一個字節出來,因此後面的bf.readLine());
讀取的就是每行少一個字節

因此,應該使用

String valueString = null;   while ((valueString=bf.readLine())!=null){            System.out.println(valueString);   }

相關文章
相關標籤/搜索