Preferred way of loading resources in Java

前端 未结 5 1660
走了就别回头了
走了就别回头了 2020-11-22 15:25

I would like to know the best way of loading a resource in Java:

  • this.getClass().getResource() (or getResourceAsStream()),
  • Thread.c
5条回答
  •  庸人自扰
    2020-11-22 15:45

    I tried a lot of ways and functions that suggested above, but they didn't work in my project. Anyway I have found solution and here it is:

    try {
        InputStream path = this.getClass().getClassLoader().getResourceAsStream("img/left-hand.png");
        img = ImageIO.read(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题