reading xml file inside a jar-package

后端 未结 4 587
悲哀的现实
悲哀的现实 2020-12-06 19:10

Here\'s my structure:

  • com/mycompany/ValueReader.class
  • com/mycompany/resources/values.xml

I can read the file in my Eclipse project, but

4条回答
  •  遥遥无期
    2020-12-06 19:56

    You can't get a File for the file because it's in a jar file. But you can get an input stream:

    InputStream in = ValueReader.class.getResourceAsStream("resources/values.xml");
    

    getResourceAsStream and getResource convert the package of the class to a file path, then add on the argument. This will give a stream for the file at path /com/mycompany/resources/values.xml.

提交回复
热议问题