How to import a Text file content to a JTextArea in a Java application?

后端 未结 5 2042
夕颜
夕颜 2020-12-18 16:19

how to import a Text file content to a JTextArea in a Java application using JFileChooser?

5条回答
  •  再見小時候
    2020-12-18 17:06

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JFileChooser jf = new JFileChooser();
         final JEditorPane document = new JEditorPane();
        int returnval=jf.showDialog(this, null);
        File file = null;
        if(returnval == JFileChooser.APPROVE_OPTION)     
         file = jf.getSelectedFile(); 
        String str ;
        try {
            byte bt[]= Files.readAllBytes(file.toPath());   
            str=new String(bt,"UTF-8");
            System.out.println(str);
        } catch (IOException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }  
    

提交回复
热议问题