java.nio.file.Path for a classpath resource

后端 未结 8 998
自闭症患者
自闭症患者 2020-11-28 02:55

Is there an API to get a classpath resource (e.g. what I\'d get from Class.getResource(String)) as a java.nio.file.Path? Ideally, I\'d like to use the fancy new Path<

8条回答
  •  时光说笑
    2020-11-28 03:26

    I wrote a small helper method to read Paths from your class resources. It is quite handy to use as it only needs a reference of the class you have stored your resources as well as the name of the resource itself.

    public static Path getResourcePath(Class resourceClass, String resourceName) throws URISyntaxException {
        URL url = resourceClass.getResource(resourceName);
        return Paths.get(url.toURI());
    }  
    

提交回复
热议问题