如何在Java中从jar读取文件? [英] How to read a file from jar in Java?
问题描述
我想读取位于我的课程路径中的 jar
之一内的XML文件。如何读取任何包含在 jar
中的文件
如果你想从你的应用程序中读取这个文件,请使用:
$ b $ pre $ InputStream input = getClass()。getResourceAsStream(/ classpath /到/我/文件);
路径以/开头,但不是文件系统中的路径,在你的类路径中。所以,如果你的文件在类路径org.xml中被称为myxml.xml,那么你的路径就像/org/xml/myxml.xml。
InputStream读取文件的内容。如果你愿意的话,你可以把它包装到Reader中。
我希望有帮助。
I want to read an XML file that is located inside one of the jar
s included in my class path. How can I read any file which is included in the jar
?
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.
这篇关于如何在Java中从jar读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!