java.nio.file.Path for a classpath resource

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

    Guessing that what you want to do, is call Files.lines(...) on a resource that comes from the classpath - possibly from within a jar.

    Since Oracle convoluted the notion of when a Path is a Path by not making getResource return a usable path if it resides in a jar file, what you need to do is something like this:

    Stream stream = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream("/filename.txt"))).lines();
    

提交回复
热议问题