繁体   English   中英

单击按钮后如何使用jFileChooser读取文件?

[英]How to read a file using jFileChooser after click of a button?

我想使用jFileChooser读取文件。 按下按钮(例如jbutton1ChooseFile)并选择所需的文件后,jFileChooser将出现。 选择完成后,将使用另一个按钮(例如jbutton2)读取用户刚刚选择的文件的内容。 因此,在单击jbutton2时,将读取所选文件。

我要发布几行代码,以便于理解我的意思:

     private void jButton1ChooseFileChooseFileActionPerformed(java.awt.event.ActionEvent evt) {                                                             
    // TODO add your handling code here:
    JFileChooser loadFile= new JFileChooser();
    loadFile.setApproveButtonText("Select File");
    loadFile.setAcceptAllFileFilterUsed(false);
    FileNameExtensionFilter f1 = new FileNameExtensionFilter("Text Files", "txt", "text","rtf","doc","docx");
    loadFile.setFileFilter(f1);
    switch (loadFile.showOpenDialog(EncDecApp.this))
             {
                case JFileChooser.APPROVE_OPTION:

                    JOptionPane.showMessageDialog(EncDecApp.this, "Selection Successfull!",
                                                 "Attention!",
                                                 JOptionPane.OK_OPTION);

                   jButton1ChooseFile.setText("File Chosen");
                   jLabelChooseFile.setText(String.valueOf(loadFile.getSelectedFile()).substring(0,30)+"...");
                   fileSelect=true;
                   break;

                case JFileChooser.CANCEL_OPTION:
                   JOptionPane.showMessageDialog(EncDecApp.this, "No file chosen",
                                                 "Attention!",
                                                 JOptionPane.OK_OPTION);
                   loadFile.setSelectedFile(null);
                   jButton1ChooseFile.setText("Browse..");
                   jLabelChooseFile.setText("Choose file to encrypt");
                   break;

                case JFileChooser.ERROR_OPTION:
                   JOptionPane.showMessageDialog(EncDecApp.this, "Error",
                                                 "Choosing File",
                                                 JOptionPane.OK_OPTION);
                   loadFile.setSelectedFile(null);
                   jButton1ChooseFile.setText("Browse..");
                   jLabelChooseFile.setText("Choose file to encrypt");
             }
    loadFile.setVisible(true);
}                               

到目前为止,它运行良好。 现在,jButton2的代码如下:

        private void jButton2EncryptEncryptActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    // TODO add your handling code here:
    //Charset charset=Charset.forName("UTF-8");
    int returnVal=loadFile.showOpenDialog(jLabel1);
    if(returnVal==loadFile.APPROVE_OPTION)
    {
      File filePath = loadFile.getSelectedFile();    

    try{
        BufferedReader in = new BufferedReader(new FileReader(filePath));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
            jTextArea1.append(line + "\n");
        }
    in.close();
    }
    catch(IOException ex)
    {
           System.err.println("Open plaintext error: "+ex);
    }     
    }
}                  

任何帮助将不胜感激。

乍看起来,问题似乎在于您正在为JFileChooser使用局部变量。 就是说,您有一行:

JFileChooser loadFile= new JFileChooser();

在您的jButton1ChooseFileChooseFileActionPerformed函数中,还尝试在jButton2EncryptEncryptActionPerformed函数中引用loadFile

为了使loadFile对象对两者都可用,您需要使loadFile对象成为这两个函数所属的类的成员。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM