classLoader.getResource doesn't work in jar file

前端 未结 2 1838
眼角桃花
眼角桃花 2021-02-05 19:12
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource(\"com/x/y/z.cfg\");
File file = new File(url.getPath());
         


        
2条回答
  •  借酒劲吻你
    2021-02-05 20:02

    You can't create a File instance, because the only file you have is the JAR. That's why getResource() returns URL. You can get stream by using URL.openStream() method to read contents.

提交回复
热议问题