java - 使用不同路径加载多个图像

标签 java eclipse

所以我尝试从同一路径加载两个图像,但名称不同。 如果我直接从图像复制路径,一切正常。 但如果我尝试从系统构建路径,则只有其中一个有效(img1)。 我尝试了在互联网上找到的几种不同方法来构建路径,但结果是相同的。

什么会导致这个问题?

public void loadImages(String nm) {
    File f = null;
    BufferedImage image = null;

    System.out.println("read img:");

    String pathName = PICTURE_PATH + this.getMyColor().toString().toLowerCase() + nm;

    // read successful this img path.
    try {
        f = new File(pathName + "North.png");
        f.canRead();
        System.out.println("\nimg1 path:" + f);
        System.out.print("img1 absolute path:" + f.getAbsolutePath());
        img1 = ImageIO.read(f);
        if (!f.canRead())
            throw new IOException("Cant read the first file");
        if (!f.exists())
            throw new IOException("Cant find the first file");
        System.out.println("Successful read img 1");
    } catch (IOException e) {
        System.out.println("Error:" + e);
    }

    // got here exception error for this img path.
    try {
        f = new File(pathName + "East.png");
        System.out.println("\nimg2 path:" + f);
        System.out.println("img2 absolute path:" + f.getAbsolutePath());
        if (!f.canRead())
            throw new IOException("Cant read the second file");
        if (!f.exists())
            throw new IOException("Cant find the second file");

        img2 = ImageIO.read(f);
        System.out.println("Successful read img 2");
    } catch (IOException e) {
        System.out.println("Error:" + e);
    }
    System.out.println("Done.");
}


//this is the relevant output for this function:
//read img:
img1 path:src\icons\‏‏silverCarNorth.png
img1 absolute path:A:\Tools\eclipse\WorkPlace\HW1\src\icons\‏‏silverCarNorth.png
Successful read img 1

img2 path:src\icons\‏‏silverCarEast.png
img2 absolute path:A:\Tools\eclipse\WorkPlace\HW1\src\icons\‏‏silverCarEast.png
Error:java.io.IOException: Cant read the second file
Done.

最佳答案

所以看起来图像有一些额外的字符.. 该网站不支持此类字符,因此我从 cmd 添加了打印屏幕。 这是来自cmd的日志: cmd logs

所以我必须重命名文件并使用 Windows 命令行手动删除多余的字符。 之后一切正常!

关于java - 使用不同路径加载多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665052/

相关文章:

android - Ubuntu 上的 Eclipse NDK 路径设置

eclipse - 是什么导致 jakarta.ws.rs.ext.RuntimeDelegate 的 Maven 依赖项中出现 java.lang.ClassNotFoundException?

java - LeJOS NXT编程声波传感器

Java——高效的、数据库感知的实例级授权?

java - Effective Java - Item 25 - Generics class cast exception 混合列表和数组

java - 在java中等于和==行为

eclipse - Team Explorer Everywhere - 从待定更改中删除忽略的文件

java - Android、Eclipse : Item in . xml 布局未在图形布局中显示

java - 在这种情况下,是否会在同一个 JVM 实例中加载同一个类两次?

java - 如何修复 'ClassNotFoundException: brave.propagation.CurrentTraceContext$ScopeDecorator' ?