2013-09-22 105 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异常。系统会告诉你,一旦你有了这些信息,那个流就关闭了,剩下的就很简单了!