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



我想使用jFileChooser读取一个文件。jFileChooser将在按下按钮(例如jbutton1ChooseFile)并选择所需的文件后出现。选择完成后,将使用另一个按钮(例如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对象是两个函数所属的类的成员。

最新更新