java.nio.file.Path for a classpath resource

后端 未结 8 1002
自闭症患者
自闭症患者 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:20

    You can not create URI from resources inside of the jar file. You can simply write it to the temp file and then use it (java8):

    Path path = File.createTempFile("some", "address").toPath();
    Files.copy(ClassLoader.getSystemResourceAsStream("/path/to/resource"), path, StandardCopyOption.REPLACE_EXISTING);
    

提交回复
热议问题