java—目录中没有填充文件的文件数组,无法找出原因

ijxebb2r  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(295)

我希望在jlist中显示从jfilechooser中选择的文件夹的内容。唯一的问题是,文件[]数组应该包含所有的文件,在选定的文件夹,由于某种原因,我无法找出为什么。。。它不会被填充。我在网上的某个地方读到过.listfiles();方法如果不将目标路径标识为目录,则返回null。。。但是我包括jfilechooser.getselectedfile().isdirectory();签入if语句。我不明白为什么会这样。。。
代码如下:

private void jBtnSelectModComActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSelectModComActionPerformed
    // TODO add your handling code here:
    try {
        SIL modificaSIL = SILmag.getSilDupaNume(jFileChooserModCom.getSelectedFile().getName().toString());
        if (jFileChooserModCom.getSelectedFile().getAbsolutePath().toString().startsWith(siluriNoi)
            && jFileChooserModCom.getSelectedFile().isDirectory()) {
            try {
                jTextFieldNumeModCom.setText(modificaSIL.getNume());
                jTextFieldTipModCom.setText(modificaSIL.getTip());
                jTextAreaDescModCom.setText(modificaSIL.getDescriere());
                String locatie = siluriNoi + "\\" + modificaSIL.toString() + "\\";
                File locatieMapa = new File(locatie);
                File[] arrayFisiere = locatieMapa.listFiles();
                System.out.println(arrayFisiere.toString()); //A check to see if the file array gets populated, but it returns null
                for(File f : arrayFisiere){
                    listaFisiereModCom.add(f.getName());
                }
                arrayFisiereModCom = new String[listaFisiereModCom.size()];
                for(int i = 0; i < listaFisiereModCom.size(); i++){
                    arrayFisiereModCom[i] = listaFisiereModCom.get(i);
                }
                Arrays.sort(arrayFisiereModCom);
                jListModCom.setModel(new javax.swing.AbstractListModel<String>() {
                    String[] strings = arrayFisiereModCom;
                    public int getSize() { return strings.length; }
                    public String getElementAt(int i) { return strings[i]; }
                });
                dosarModCom.add(new File(jFileChooserModCom.getSelectedFile().getPath()));
                jFileChooserModCom.rescanCurrentDirectory();
                jFileChooserModCom.revalidate();
            } catch(NullPointerException ex) {
                jLabelAvertismenteModCom.setText("<html><center>Trebuie sa selectezi o comanda!</center></html>");
            }
        } else {
            jLabelAvertismenteModCom.setText("<html><center>Comenzile set pot modifica doar<br> din locatia la care ai access!</center></html>");
         }
    } catch(NullPointerException ex) {
        jLabelAvertismenteModCom.setText("<html><center>Trebuie sa selectezi o comanda!</center></html>");
    } catch (SQLException ex) {
        java.util.logging.Logger.getLogger(MeniuTaburi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_jBtnSelectModComActionPerformed

以及我在jfilechooser上实现的一些更改:

jFileChooserModCom.setCurrentDirectory(new File(siluriNoi)); //siluriNoi is a String path to a particular directory
    jFileChooserModCom.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    jFileChooserModCom.setControlButtonsAreShown(false);
    jFileChooserModCom.rescanCurrentDirectory();
    jFileChooserModCom.revalidate();
iqxoj9l9

iqxoj9l91#

&& jFileChooserModCom.getSelectedFile().isDirectory()) {
        try {
            jTextFieldNumeModCom.setText(modificaSIL.getNume());
            jTextFieldTipModCom.setText(modificaSIL.getTip());
            jTextAreaDescModCom.setText(modificaSIL.getDescriere());
            String locatie = siluriNoi + "\\" + modificaSIL.toString() + "\\";
            File locatieMapa = new File(locatie);
            File[] arrayFisiere = locatieMapa.listFiles();

我包括jfilechooser.getselectedfile().isdirectory();检查
是的,但实际上从未使用getselectedfile()。
您使用:

String locatie = siluriNoi + "\\" + modificaSIL.toString() + "\\";
File locatieMapa = new File(locatie);
File[] arrayFisiere = locatieMapa.listFiles();

您的代码应该类似于:

File selectedDirectory = fileChooser.getSelectedFile();
System.out.println(selectedDirectory); // to verify the complete file path
File[] listOfFiles = selectedDirector.listFiles();

相关问题