2014-08-29 22 views
-3

當使用filereader類的結果在我的文本區域爲空,我的代碼有什麼問題?我的代碼使用ubuntu時出現了什麼問題,在我的文本區域jar1中給我null?

try{  
     File filename=new File(jttex1.getText()); 
     FileReader reader=new FileReader(filename); 
     BufferedReader br=new BufferedReader(reader); 
     String data; 

      long m=filename.length(); 
     while((data=br.readLine())!=null) ; { 
     jar1.append(data+"/"); 
     } 
     br.close(); 
     reader.close(); 
     p("complete reading "+m); 


     }catch(IOException ex){ 

      System.err.println(ex.getMessage()); 
     } 
+0

應該如何我們知道,你沒有發佈任何有用的,不完整的代碼,不會編譯,沒有堆棧跟蹤沒有錯誤信息沒有! Google SSCCE。 – 2014-08-29 13:29:00

回答

2

因爲你有一個;while-loop。這的出發導致循環的完成,直到data equals null

while((data=br.readLine())!=null) ; { 
.... 
.... 
} 
//correct this by removing the semi-colon and your code will perform as intended! 
+0

@ mizomizo - 如果有幫助,請接受答案! – 2014-08-29 13:34:07

+1

其中...。它讀取所有重複';'的行,然後以'{...}'開始,'data'爲'null'。 – 2014-08-29 13:34:46

+1

@JoopEggen * //通過刪除分號*** *來糾正此* ***,並且您的代碼將按預期執行!* – BackSlash 2014-08-29 13:40:29

1

這分號從它身上

while ((data=br.readLine())!=null) ; { 
            ^

導致data分離while語句將字符串化到「空」(由於循環的退出條件)

+0

真的很感謝reimeus – mizomizo 2014-08-29 16:08:47

+0

感謝joop和suman – mizomizo 2014-08-29 16:09:35

+0

@ mizomizo - Stack Overflow感謝的預期行爲是通過接受最有幫助的答案來完成的。請接受這裏給出的兩個答案中的一個! – 2014-08-29 17:58:28

相關問題