如何使用javafx从目录中获取所有文件

How to get all files from a directory using javafx

我是 JavaFx 的新手,我无法从一个目录中获取所有文件(我需要使用由 javafx 构建的界面来显示一些图片)。我一直在尝试使用 listFiles() 但在显示以进行测试时我一直在获取空值。 这是代码:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.shape.Polygon;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class Controller implements Initializable{

    private String path;
    private ArrayList<File> files;
    private String[] extens;

    @FXML
    public Polygon right_arrow;
    public Polygon left_arrow;
    public Button edit_button;
    public ImageView home_button;
    public TextField search_box;
    public TextArea tag_box;
    public ChoiceBox language_box;
    public Label photo_title;
    public Label photo_number;
    public ImageView photo_box;
    public Image home_picture;

    public void goToEditPage (ActionEvent event) throws Exception {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Edit.fxml"));

            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.setScene(new Scene(root1));
            stage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public void goToHomePage (){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("home.fxml"));

            Parent root1 = fxmlLoader.load();
            Stage stage = new Stage();
            stage.setScene(new Scene(root1));
            stage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


    public Image getPicture(){


        /*home_picture = new Image(String.valueOf(getClass().getResource("home.png")));
        */return home_picture;
    }

    private void photo (String pathGet){
        path = pathGet;
        files = new ArrayList<File>();

        File repo = new File (path);

        File[] fileList = repo.listFiles();
        for (int i=0; i<1; i++) {
            System.out.println(repo.list());
        }
       // System.out.println(fileList.length);
        /*int nb = fileList.length;
        String ext = "";
        int s = -1;

        for (int i=0; i<nb; i++){
            if (fileList[i].isFile()){
                s = fileList[i].getName().lastIndexOf(".");
                if (s>-1) {
                    ext = fileList[i].getName().substring(s + 1);
                }
                if (ext=="jpg"||ext=="png"||ext=="jpeg"){
                    files.add(fileList[i]);
                }
            }
        }*/
    }


    public File getPhotoById(int id) {
        id--;
        return this.files.get(id);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        photo("sample/photos/album_1");
        //photo_box.setImage(new Image(files.get(1).toURI().toString()));
    }
}

我想我没有以正确的方式使用 listFiles(),但我不知道如何实际使用。 我的文件夹 album_1 包含 3 张 .png 格式的图片。但是,当我尝试这个示例时,我什么也没得到,就好像文件夹是空的一样。 我希望有人能够帮助我。 谢谢你的时间。

您的 for 循环是错误的。遍历返回的文件列表的正确方法是

for (int i = 0 ; i < fileList.length ; i++) {
    System.out.println(filesList[i]);
}

或(更好):

for (File f : fileList) {
    System.out.println(f);
}

请注意 documentation for listFiles() 表示:

An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

因此,如果您在此处收到空指针异常,那是因为 repo 不是目录。您可能想先检查一下:

if (repo.isDirectory()) {
    File[] fileList = repo.listFiles();
    // ...
}