如何逐行将txt文件加载到JTextArea中

时间:2016-07-10 14:02:03

标签: java

我想将txt文件加载到JtextArea中,总是显示错误消息,尽管我已尝试使用apends()或Settext() 下面是代码

`  JmenuItem.addActionListener(
            new ActionListener()
            {   public void actionPerformed(ActionEvent e)
                {  
                try
                {
                JFileChooser open = new JFileChooser();
                int option = open.showOpenDialog(open);
                File f1 = new File(open.getSelectedFile().getPath());
                FileReader fr = new FileReader(f1);
                BufferedReader br = new BufferedReader(fr);
                String s=null;
                while((s=br.readLine())!=null)
                {   textArea.setText(s);
                    textArea.setText("\n");

                }
                fr.close();
                } catch(FileNotFoundException z){       
    JOptionPane.showMessageDialog(null, "file not found");
                  }catch (IOException z) { z.printStackTrace();  }
                 }
                }           

            );
    JTextArea textArea = new JTextArea(40,40);
    }
    }

`

0 个答案:

没有答案