从类路径资源(XML 文件)获取输入流 [英] Getting the inputstream from a classpath resource (XML file)

查看:40
本文介绍了从类路径资源(XML 文件)获取输入流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java web应用程序中,假设我想获取一个XML文件的InputStream,该文件位于CLASSPATH(即sources文件夹内),我该怎么做?>

In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sources folder), how do I do it?

推荐答案

ClassLoader.getResourceAsStream().

正如下面的评论中所述,如果您处于多ClassLoader 环境(例如单元测试、webapps 等)中,您可能需要使用 Thread.currentThread().getContextClassLoader().请参阅http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/230838837307593_21307593_2308182.

As stated in the comment below, if you are in a multi-ClassLoader environment (such as unit testing, webapps, etc.) you may need to use Thread.currentThread().getContextClassLoader(). See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388.

这篇关于从类路径资源(XML 文件)获取输入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆