Load resources from classpath in Java--reference
In general classpath is the path where JVM can find .class files and resources of your application and in this tutorial we will see how to load resources such as .properties files that are on classpath.
Class' getResourceAsStream()
One way to load a resource is with getResourceAsStream() method of Class class.As an example consider the case where a .properties file is at a folder named resources.We could use getResourceAsStream method as shown in the below snippet.
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class ResourceLoader { public static void main(String args[]) throws IOException{ InputStream resourcesStream = ResourceLoader.class.getResourceAsStream("/resources/resources.properties");
Properties properties = new Properties();
properties.load(resourcesStream);
System.out.println(properties.get("property.name"));
} }
Using ClassLoader's getResourceAsStream()
Another way to load a resource is by using Classloader's getResourceAsStream() method.As an example consider the below snippet:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class ResourceLoader { public static void main(String args[]) throws IOException{ InputStream resourcesStream = ResourceLoader.class.getClassLoader().getResourceAsStream("resources/resources.properties");
Properties properties = new Properties();
properties.load(resourcesStream);
System.out.println(properties.get("property.name"));
} }
Class' vs ClassLoader's getResourceAsStream()
Difference between Class' and ClassLoader's getReourceAsStream() is in the way the path-to-resrouce is defined.In the case of Class class getResourcesAsStream() accept's either the relative or the absoulute path of the resource.On the other hand ClassLoader's getResourceAsStream() method accept's only the absolute path to the resource and because of this,if we used "/resources/resources.properties" wouldn't be found and getResourceAsStream() would return null
http://www.java-only.com/LoadTutorial.javaonly?id=118
Load resources from classpath in Java--reference的更多相关文章
- URL to load resources from the classpath in Java
In Java, you can load all kinds of resources using the same API but with different URL protocols: fi ...
- Java Reference简要概述
@(Java)[Reference] Java Reference简要概述 Reference对象封装了其它对象的引用,可以和普通的对象一样操作. Java提供了四种不同类型的引用,引用级别从高到低分 ...
- 出现错误:Unable to load configuration. - action - file:/E:/Java/Tomcat7.0/apache-tomcat-7.0.68-windows-x64/apache-tomcat-7.0.68/webapps/SSH2Integrate/WEB-INF/classes/struts.xml:8:43
严重: Exception starting filter struts2 Unable to load configuration. - action - file:/E:/Java/Tomcat7 ...
- Java Reference 源码分析
@(Java)[Reference] Java Reference 源码分析 Reference对象封装了其它对象的引用,可以和普通的对象一样操作,在一定的限制条件下,支持和垃圾收集器的交互.即可以使 ...
- The SSL certificate used to load resources from xxx will be distrusted in M70.
今天在浏览网站的时候遇到如下报警信息: The SSL certificate used to load resources from https://xxx.com will be distrust ...
- java Reference
相关讲解,参考: Java Reference 源码分析 Java Reference详解 Reference: // 名称说明下:Reference指代引用对象本身,Referent指代被引用对象 ...
- Java 实例 - 如何执行指定class文件目录(classpath) Java 实例 J
Java 实例 - 如何执行指定class文件目录(classpath) Java 实例 如果我们 Java 编译后的class文件不在当前目录,我们可以使用 -classpath 来指定class ...
- Java Reference & ReferenceQueue一览
Overview The java.lang.ref package provides more flexible types of references than are otherwise ava ...
- Java Reference核心原理分析
本文转载自Java Reference核心原理分析 导语 带着问题,看源码针对性会更强一点.印象会更深刻.并且效果也会更好.所以我先卖个关子,提两个问题(没准下次跳槽时就被问到). 我们可以用Byte ...
随机推荐
- Java多线程初学者指南(9):为什么要进行数据同步
Java中的变量分为两类:局部变量和类变量.局部变量是指在方法内定义的变量,如在run方法中定义的变量.对于这些变量来说,并不存在线程之间共享的问题.因此,它们不需要进行数据同步.类变量是在类中定义的 ...
- 深入解析java虚拟机-jvm运行机制
转自oschina 一:JVM基础概念 JVM(Java虚拟机)一种用于计算设备的规范,可用不同的方式(软件或硬件)加以实现.编译虚拟机的指令集与编译微处理器的指令集非常类似.Java虚拟机包括一套字 ...
- JAVA 反序列化攻击
Java 反序列化攻击漏洞由 FoxGlove 的最近的一篇博文爆出,该漏洞可以被黑客利用向服务器上传恶意脚本,或者远程执行命令. 由于目前发现该漏洞存在于 Apache commons-collec ...
- UVA 1513 Movie collection
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 200010 #define l ...
- TDBGrideh表头自动排序设置
自动显示标题行的升降排序标志符(▽降序△升序)并做相应排序DBGridEh组件可以在标题行单元格中显示小三角形升.降排序标志符图片,在运行时可点击标题行,图片自动切换并做相应排序. 具体属性设置如下: ...
- 制作LOGO的35种方法
A logo design is really a graphical element (ideogram, symbol, emblem, icon, sign) that, along with ...
- bzoj1984
树链剖分在边上的应用比维护点稍微麻烦一点,是对每条边标号,并且要记录每个点父亲边的编号和重儿子然后注意各种细节线段树上和bzoj1858的维护方法类似,覆盖的优先级高于加具体见程序,完全是为了提升状态 ...
- WebView loadData出错(奇怪的设计)
今天遇到一个奇怪的问题. 我使用WebView加载一个网页. 方法1. 直接使用 loadUrl() 方法,没有问题.完全可以.方法2. 使用loadData()方法,出现问题,无法显示.方法3. ...
- HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契
题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...
- Java编程常见问题汇总
每天在写Java程序,其实里面有一些细节大家可能没怎么注意,这不,有人总结了一个我们编程中常见的问题.虽然一般没有什么大问题,但是最好别这样做.另外这里提到的很多问题其实可以通过Findbugs( h ...