加载Java类路径中的XML文件 [英] Loading XML File which is in classpath in Java
问题描述
我需要将类路径中存在的xml文件加载到我的Java应用程序中。
I need to load a xml file present in classpath into my java applicatio. Can any one give suggestions on this.
谢谢,
Narendra
Thanks,
Narendra
推荐答案
使用 ClassLoader.getResourceAsStream
或 Class.getResourceAsStream
,然后按需加载对于任何其他 InputStream
。
(使用 ClassLoader $ c之间的区别$ c>版本和
Class
版本是解决相对资源路径的方式,而 ClassLoader
版本是总是有效地视为绝对路径;对于 Class
版本,如果路径不是以 /开头,则将其视为相对于该类包的路径。)
(The difference between using the ClassLoader
version and the Class
version is how "relative" resource paths are resolved. With the ClassLoader
version, it's always effectively treated as an absolute path; with the Class
version, if the path doesn't start with "/" it's treated as relative to the package of that class.)
这篇关于加载Java类路径中的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!