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

时间: 2024-08-10 02:10:02

Load resources from classpath in Java--reference的相关文章

Eclipse运行时提示“Failed to load the JNI shared library /Java/jre6/bin/client/jvm.dll”的一个解决方案

出现这个问题的一个原因是eclipse需要的32位jdk,你的环境变量中配的是64位jdk 于是有人建议,重装个32位的jdk,把环境变量换成32位的jdk,但如果你的其它程序需要64位jdk的话,这肯定不是一个好办法 还有一种方法是在 eclipse根目录下有个eclipse.ini文件,添加个 -vm参数 -vm C:/Java32/jdk1.7.0_45/bin/javaw.exe      //这是我的32位jdk 注意位置 -startup plugins/org.eclipse.eq

Java Reference & ReferenceQueue一览

Overview The java.lang.ref package provides more flexible types of references than are otherwise available, permitting limited interaction between the application and the Java Virtual Machine (JVM) garbage collector. It is an important package, centr

Java 实例 - 如何执行指定class文件目录(classpath) Java 实例 J

Java 实例 - 如何执行指定class文件目录(classpath)  Java 实例 如果我们 Java 编译后的class文件不在当前目录,我们可以使用 -classpath 来指定class文件目录: C:> java -classpath C:\java\DemoClasses HelloWorld 以上命令中我们使用了 -classpath 参数指定了 HelloWorld 的 class 文件所在目录. 如果class文件在jar文件中,则命令如下: c:> java -cla

出现错误: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.0/apache-tomcat-7.0.68-windows-x64/apache-tomcat-7.0.68/webapps/SSH2Integrate/WEB-INF/classes/struts.xml:8:43 at org.apache.struts2.dispatcher.Dispat

理解java reference

Java世界泰山北斗级大作<Thinking In Java>切入Java就提出“Everything is Object”.在Java这个充满Object的世界中,reference是一切谜题的根源,所有的故事都是从这里开始的. Reference是什么? 如果你和我一样在进入Java世界之前曾经浪迹于C/C++世界,就一定不会对指针陌生.谈到指针,往日种种不堪回首的经历一下子涌上心头,这里不是抱怨的地方,让我们暂时忘记指针的痛苦,回忆一下最初接触指针的甜蜜吧!还记得你看过的教科书中,如何讲

Java Reference Types

References Java provides two different types/classes of Reference Objects: strong and weak. Weak Reference Objects can be further divided into soft and phantom. Strong Reference StringBuilder builder = new StringBuilder(); This is the default type/cl

Java Reference 源码分析

Reference对象封装了其它对象的引用,可以和普通的对象一样操作,在一定的限制条件下,支持和垃圾收集器的交互.即可以使用Reference对象来引用其它对象,但是最后还是会被垃圾收集器回收.程序有时候也需要在对象回收后被通知,以告知对象的可达性发生变更.  Java提供了四种不同类型的引用,引用级别从高到低分别为FinalReference,SoftReference,WeakReference,PhantomReference.其中FinalReference不对外提供使用.每种类型对应着

What Influences Method Call Performance in Java?--reference

reference from:https://www.voxxed.com/blog/2015/02/too-fast-too-megamorphic-what-influences-method-call-performance-in-java/ Whats this all about then? Let’s start with a short story. I proposed a change on the a Java core libs mailing list to overri

java reference(转)

http://blog.163.com/[email protected]/blog/static/112987702200962211145825/ 在Java中的引用类型,是指除了基本的变量类型之外的所有类型,所有的类型在内存中都会分配一定的存储空间(形参在使用的时候也会分配存储空间,方法调用完成之后,这块存储空间自动消失), 基本的变量类型只有一块存储空间(分配在stack中), 而引用类型有两块存储空间(一块在stack中,一块在heap中), 方法形参的值传递(引用)是指形参和传进来的