Java中getResourceAsStream的用法
首先,Java中的getResourceAsStream有以下几种:
1. Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从
ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。
2. Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由
ClassLoader获取资源。
3. ServletContext. getResourceAsStream(String path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/'开头无所谓,
当然这和具体的容器实现有关。
4. Jsp下的application内置对象就是上面的ServletContext的一种实现。
其次,getResourceAsStream 用法大致有以下几种:
第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类me.class ,同时有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("myfile.xml");
第二:在me.class目录的子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.y.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("file/myfile.xml");
第三:不在me.class目录下,也不在子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("/com/x/file/myfile.xml");
总结一下,可能只是两种写法
第一:前面有 “ / ”
“ / ”代表了工程的根目录,例如工程名叫做myproject,“ / ”代表了myproject
me.class.getResourceAsStream("/com/x/file/myfile.xml");
第二:前面没有 “ / ”
代表当前类的目录
me.class.getResourceAsStream("myfile.xml");
me.class.getResourceAsStream("file/myfile.xml");
分享到:
相关推荐
主要介绍了Java 使用getClass().getResourceAsStream()方法获取资源的相关资料,这里主要讲解哪种方式可以获取到文件资源,需要的朋友可以参考下
Java加载资源文件的两种方法getResource与getResourceAsStream
InputStream is = com.aspose.words.Document.class.getResourceAsStream("/com.aspose.words.lic_2999.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; is.close(); } ...
InputStream is = Demo1.class.getResourceAsStream("/students.xml");// src目录下 // 1. 获得文档解析器工厂对象 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // 2. ...
InputStream is = com.aspose.words.Document.class .getResourceAsStream("/com.aspose.words.lic_2999.xml"); License asposeLicense = new License(); asposeLicense.setLicense(is); is.close(); } catch ...
刚开始简单破解了一下。 生成PDF都没问题,后来发现生成... InputStream is = XXX.class.getResourceAsStream("/pdflicense.xml"); license.setLicense(is); is.close(); 其中pdflicense.xml已经包含在jar文件里面
1。使用java.util.Properties类的load()方法 示例: InputStream in = lnew ...InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in);
InputStream inputStream = CommonTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml"); SQLSessionFactory SQLSessionFactory = new SQLSessionFactoryBuilder().build(inputStream)...
List<WeatherInfos> infos = weatherService.getWeatherInfos(MainActivity.class.getClassLoader().getResourceAsStream("z.xml")); StringBuffer sb = new StringBuffer(); for(WeatherInfos info : infos){ ...
InputStream inStream = DaoFactory.class.getClassLoader() .getResourceAsStream("daoconfig.properties"); prop.load(inStream); String userDaoClass = prop.getProperty("userDaoClass"); ...
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
aspose.slides-16.10.0.jar破解版,亲测可用。...InputStream input = AsposeUtil.class.getResourceAsStream("license.xml"); License license = new com.aspose.slides.License(); license.setLicense(input);
Document document = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName)); Element root = document.getRootElement(); List list = XPath.selectNodes(root, "/beans/bean"); for...
in = MyTask.class.getClassLoader().getResourceAsStream("db.properties"); prop.load(in); this.sql_ip=prop.getProperty("DB_IP").trim(); this.sql_db_name=prop.getProperty("DB_NAME").trim(); this.sql...
InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); try { addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); } catch...
InputStream in=getClass().getResourceAsStream(propFileName); prop.load(in); //通过输入流对象加载Properties文件 dbClassName = prop.getProperty("DB_CLASS_NAME"); //获取数据库驱动 dbUrl =...
java getResourceAsStream种类,分我所知道的3种情况
InputStream license = ChangePDF.class.getClassLoader().getResourceAsStream("\\license.xml"); try { new License().setLicense(license); } catch (Exception e) { e.printStackTrace(); } Document ...
ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(ThumbnailatorImageUtil.class.getClassLoader().getResourceAsStream("ISOcoated_v2_300_eci.icc"))); ColorConvertOp op = new ...
is = new Document().getClass().getResourceAsStream("/resources/aspose.word.license.xml"); if(is==null) throw new RuntimeException("Cannot find licenses file. Please contact wdmsyf@yahoo....