2013-09-22 106 views
0

我想要做的是非常基本的:我有一個包含按鈕的界面;當我按下該按鈕時,我希望我的程序從文本文件中讀取下一行並將其顯示在文本字段中。但沒有任何反應,而且我有一種感覺,那是因爲它沒有正確讀取我的文件:\請幫助,我是Java世界中的一名完全新手,我甚至很高興我擺脫了編譯器錯誤(yay me !)但這是更糟的,因爲現在我不知道谷歌:))爲什麼java代碼不工作?

package practice; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.InputStreamReader; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 

public class MyApp extends JFrame { 
    JButton button; 
    JTextArea afisaj; 

    MyApp(){ 
     setTitle("MyApp"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     init(); 
     setSize(500,500); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    public void init(){ 
     this.setLayout(null); 

     button = new JButton("Read more"); 
     afisaj = new JTextArea(); 

     button.setBounds(200,50,100,30); 
     add(button); 

     afisaj.setBounds(40,100,400,300); 
     add(afisaj); 
    } 

public static void main(String[] args){ 
    final MyApp m = new MyApp(); 

    File f = new File("C:\\myfile.txt"); 
    BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); 
    try{ 
    b = new BufferedReader(new FileReader(f)); 
    } 
    catch (FileNotFoundException e){System.out.println("error 1")} 

    final BufferedReader bf = b; 

     m.button.addActionListener(new ActionListener(){  
     public void actionPerformed(ActionEvent e){ 
      String s = new String();   
      try{ 
       if ((s=bf.readLine())!= null){ 
        m.afisaj.append(s); 
       } 
      } 
      catch (Exception ee){System.out.println("error 2")} 
     }  
    }); 

    try{ 
    bf.close(); 
    } 
    catch (Exception e1){System.out.println("error 3")}; 

} 

} 
+6

第1步:**不要吞下異常**。至少打印出來。 – Mat

+0

@Mat偉大的建議,但我更傾向於(明確)stacktrace的基礎上,這是提供了極好的細節。 OP - 將catch(Exception e){..']形式的代碼更改爲catch(Exception e){e.printStackTrace(); //非常翔實! ..' –

+0

ook,我得到了第2個錯誤。但我不知道爲什麼:( – user2707860

回答

0

問題是,你定義的流在try塊中關閉。所以,當你嘗試閱讀它時,Stream已關閉。

0

1,數據不能共享,BF在click事件沒有得到到

final BufferedReader bf = b; 

2,代碼修改如下以實現您的結果

public static void main(String[] args) { 
    final MyApp m = new MyApp(); 
    m.button.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      File f = new File("C:\\myfile.txt"); 
      BufferedReader b = new BufferedReader(new InputStreamReader(
        System.in)); 
      try { 
       b = new BufferedReader(new FileReader(f)); 
      } catch (FileNotFoundException ee) { 
      } 

      String s = new String(); 
      try { 
       while ((s = b.readLine()) != null) { 
        m.afisaj.append(s); 
       } 
       b.close(); 
      } catch (Exception ee) { 
      } 
     } 
    }); 
} 
0

你應該關閉流,當你關閉窗口。現在您在註冊該按鈕的事件偵聽器後關閉它。在你真正按下按鈕之前。

public static void main(String[] args) throws Exception { 
    final MyApp m = new MyApp(); 

    File f = new File("myfile.txt"); 
    BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); 
    b = new BufferedReader(new FileReader(f)); 

    final BufferedReader bf = b; 

    m.button.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     String s = new String(); 
     try { 
     if ((s = bf.readLine()) != null) { 
      m.afisaj.append(s); 
     } 
     } catch (Exception ex) { 
     throw new RuntimeException(ex); 
     } 
    } 
    }); 

    m.addWindowListener(new WindowAdapter() { 
    @Override 
    public void windowClosing(WindowEvent e) { 
     try { 
     bf.close(); 
     } catch (Exception ex) { 
     throw new RuntimeException(ex); 
     } 
    } 
    }); 
} 

請停止swalowing異常。系統會告訴你,一旦你有了這些信息,那個流就關閉了,剩下的就很簡單了!