How to read a file from jar in Java?

后端 未结 5 1287
我寻月下人不归
我寻月下人不归 2020-11-22 05:45

I want to read an XML file that is located inside one of the jars included in my class path. How can I read any file which is included in the jar?<

5条回答
  •  名媛妹妹
    2020-11-22 06:24

    If you want to read that file from inside your application use:

    InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");
    

    The path starts with "/", but that is not the path in your file-system, but in your classpath. So if your file is at the classpath "org.xml" and is called myxml.xml your path looks like "/org/xml/myxml.xml".

    The InputStream reads the content of your file. You can wrap it into an Reader, if you want.

    I hope that helps.

提交回复
热议问题