Getting Files in Directory Fails

65 Views Asked by At

I'm trying to load in an Image array to display, which size depends on the number of images found in a specific directory. However, Java (16) can't seem to find any file in the directory at all, although it seems like it finds the right directory.
I'm building the project using Maven through IntelliJ, which automatically looks in a designated "resources" folder first. Also I'm on Windows.

The structure looks like this:
resources > graphics > rooms > 1 > CheckMark.png, QuestionMark.png

What I want, is to get both of those images.

The code looks like this:

    private final String graphicsDir = "/graphics";
    private final String roomDir = "/rooms";
    private final String MISCDir = "/MISC";

    public Image[] getRoomGraphics(int id){
        String dirPath = graphicsDir + roomDir + "/" + id;
        ArrayList<Image> imagesFound = new ArrayList<>();

        File dir = new File(dirPath);
        File[] filesInDir = dir.listFiles();

        if(filesInDir.length < 0) {

            for (File f : filesInDir) {
                imagesFound.add(new Image(getClass().getResourceAsStream(f.getAbsolutePath())));
            }

        }else{
            return new Image[]{getGraphicsNotFound()};
        }

        Image[] output = new Image[imagesFound.size()];
        for(int i = 0; i < imagesFound.size(); i++){
            output[i] = imagesFound.get(i);
        }

        return output;
    }

The function getGraphicsNotFound() successfully returns an image of a question mark.
I've tried a lot of things: dir.isDirectory() returns true, the folders and files exist and are accessible through Explorer, the dir.getAbsolutePath() is wrong however: It returns: /resources/graphics/rooms/1 Whereas the actual absolute path is D:/.../resources/...

As for me loading the ArrayList into a fixed size array at the end: I want to try something with animations later, so that's why. It shouldn't make a difference.

1

There are 1 best solutions below

1
G. B. Wanscher On

Aight, I seem to have fixed it by accident. It seems that Maven's auto-selecting of the "resource" directory, doesn't work if you want to reference files in other directories through said directory. The file paths get screwed up since when you get to the point where you reference the files in the directory in the resource directory, you'll need the canonical paths, but the paths provided at this point is from Maven's resource folder. Thus Java can't find anything.

Solution: Provide the filepath from project root if you want to find a directory in the resource directory. And then later be able to reference the files in this new directory.

The fix looks like this:

    private final String graphicsDir = "/graphics";
    private final String projRootToGraphicsDir = "src/main/resources" + graphicsDir;
    private final String roomDir = "/rooms";
    private final String MISCDir = "/MISC";

    public Image[] getRoomGraphics(int id){
        String dirPath = projRootToGraphicsDir + roomDir + "/" + id;

        ArrayList<Image> imagesFound = new ArrayList<>();
        File dir = new File(dirPath);
        File[] filesInDir = dir.listFiles();

        assert filesInDir != null;
        if(filesInDir.length > 0) {
            
            for (File f : filesInDir) {
                imagesFound.add(new Image(f.toURI().toString()));
            }

        }else{
            return new Image[]{getGraphicsNotFound()};
        }

        Image[] output = new Image[imagesFound.size()];
        for(int i = 0; i < imagesFound.size(); i++){
            output[i] = imagesFound.get(i);
        }

        return output;
    }