Load resources from classpath in Java--reference
2014-06-07 23:34
218 查看
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.
Using ClassLoader's getResourceAsStream()
Another way to load a resource is by using Classloader's getResourceAsStream() method.As an example consider the below snippet:
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
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
相关文章推荐
- java.lang.UnsatisfiedLinkError: Couldn't load memchunk from loader dalvik.system.PathClassLoader
- java.lang.UnsatisfiedLinkError: Couldn't load xxx from loader dalvik.system.PathClassLoader的解决方法
- java.lang.UnsatisfiedLinkError: Couldn't load xxx from loader dalvik.system.PathClassLoader的终极解决方法
- Exception from System.loadLibrary(smjavaagentapi) java.lang.UnsatisfiedLinkError: no smjavaagentapi in java.library.path
- No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/package/java/hibernat
- 如何解决java.lang.UnsatisfiedLinkError: Couldn't load x from loader dalvik.system.PathClassLoader
- java.lang.UnsatisfiedLinkError: Couldn't load slbank from loader dalvik.system.PathClassLoader
- java.lang.UnsatisfiedLinkError: Couldn't load dnlocal from loader dalvik.system.PathClassLoader
- java.lang.UnsatisfiedLinkError: Couldn't load hyphenate_av from loader dalvik.system.PathClassLoader
- java.lang.UnsatisfiedLinkError: Couldn't load BaiduMapSDK_v2_3_1 from loader dalvik.system.PathClass
- AS找不到.so文件 java.lang.UnsatisfiedLinkError: Couldn't load from loader dralvik.system.PathClassLoade
- java.lang.UnsatisfiedLinkError: Couldn't load hyphenate_av from loader dalvik.system.PathClassLoader
- Exception from System.loadLibrary(smjavaagentapi) java.lang.UnsatisfiedLinkError: no smjavaagentapi in java.library.path
- java.lang.UnsatisfiedLinkError: Couldn't load easemob_jni from loader dalvik.system.PathClassLoader
- Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add
- Location of javaagent jar in bootclasspath
- JAVA错误:AXIS Web Service Problem: No compiler found in your classpath! (you may need to add ‘tools.ja
- No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath
- How to use virtual path providers to dynamically load and compile content from virtual paths in
- java.lang.unsupportedclassversionerror bad version number in .class file unable to load class