获取目录中的文件失败

发布于 2025-01-09 06:00:33 字数 1466 浏览 0 评论 0原文

我正在尝试加载要显示的图像数组,其大小取决于特定目录中找到的图像数量。 然而,Java(16)似乎根本无法在目录中找到任何文件,尽管它似乎找到了正确的目录。
我正在通过 IntelliJ 使用 Maven 构建项目,它首先自动查找指定的“资源”文件夹。我也是 Windows 上的。

结构如下所示:
资源>>图形>房间> 1> CheckMark.png、QuestionMark.png

我想要的是获得这两个图像。

代码如下所示:

    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;
    }

函数 getGraphicsNotFound() 成功返回问号图像。
我已经尝试了很多事情: dir.isDirectory() 返回 true,文件夹和文件存在并且可以通过资源管理器访问,但是 dir.getAbsolutePath() 是错误的: 它返回:/resources/graphics/rooms/1 而实际的绝对路径是 D:/.../resources/...

至于我最后将 ArrayList 加载到固定大小的数组中:我想稍后尝试一些动画,所以这就是原因。应该没有什么区别。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2025-01-16 06:00:33

好吧,我似乎不小心修复了它。
如果你想通过该目录引用其他目录中的文件,Maven 自动选择“资源”目录似乎不起作用。
文件路径会搞砸,因为当您引用资源目录中的目录中的文件时,您将需要规范路径,但此时提供的路径来自 Maven 的资源文件夹。
因此Java找不到任何东西。

解决方案:
如果您想在资源目录中查找目录,请提供项目根目录的文件路径。然后就可以引用这个新目录中的文件。

修复如下所示:

    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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文