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的更多相关文章

  1. 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 ...

  2. Java Reference简要概述

    @(Java)[Reference] Java Reference简要概述 Reference对象封装了其它对象的引用,可以和普通的对象一样操作. Java提供了四种不同类型的引用,引用级别从高到低分 ...

  3. 出现错误: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 ...

  4. Java Reference 源码分析

    @(Java)[Reference] Java Reference 源码分析 Reference对象封装了其它对象的引用,可以和普通的对象一样操作,在一定的限制条件下,支持和垃圾收集器的交互.即可以使 ...

  5. 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 ...

  6. java Reference

    相关讲解,参考: Java Reference 源码分析 Java Reference详解 Reference: // 名称说明下:Reference指代引用对象本身,Referent指代被引用对象 ...

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

    Java 实例 - 如何执行指定class文件目录(classpath)  Java 实例 如果我们 Java 编译后的class文件不在当前目录,我们可以使用 -classpath 来指定class ...

  8. Java Reference & ReferenceQueue一览

    Overview The java.lang.ref package provides more flexible types of references than are otherwise ava ...

  9. Java Reference核心原理分析

    本文转载自Java Reference核心原理分析 导语 带着问题,看源码针对性会更强一点.印象会更深刻.并且效果也会更好.所以我先卖个关子,提两个问题(没准下次跳槽时就被问到). 我们可以用Byte ...

随机推荐

  1. 限制sqlserver最大内存后无法连接-EXEC sp_configure max server memory

    在sql server 中设置了过小的 "max server memory"最大内存后,sqlserver可启动,但是无法连接. 网络上流行的"sqlserver 内存 ...

  2. 【HDOJ】2602 Bone Collector

    DP. #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct { in ...

  3. pcDuino安装vnc进行远程控制

    准备工作: 已经刷好的 pcduino : 点此购买   可选用显示器或者用ssh连接,ssh连接参考 无显示器刷机与使用 1.安装x11vnc 输入下面的命令: sudo apt-get insta ...

  4. 漫谈CSS的渲染效率

    总结了部分所学.所听.所看.所问的一些CSS写作经验,书写高效的CSS - 漫谈CSS的渲染效率,它们与渲染效率及所占用消耗的资源有一定的关 联.部分为自己理解所写,不排除会有错漏,欢迎提供更好的意见 ...

  5. Android Weekly Notes Issue #238

    Android Weekly Issue #238 January 1st, 2017 Android Weekly Issue #238 本期内容包括: Firebase发送Notification ...

  6. 数学:lucas定理的总结

    今天考试的题目中有大组合数取模,不会唉,丢了45分,我真是个弱鸡,现在还不会lucas. 所以今天看了一下,定理差不多是: (1)Lucas定理:p为素数,则有: 即:lucas(n,m,p)=c(n ...

  7. SVG事件响应

    1 UIEvents(用户界面事件)  focusin(onfocusin):一个元素获得焦点(例如,一段文本被选中)  focusout(onfocusout):一个元素失去焦点(例如,一段文本 ...

  8. javaweb要点复习 jsp和servlet

    jsp:就是java server page ,  html嵌入java  ,所以更方面显示(V) serlet,就是服务器端小程序 java中嵌入html,更方面业务处理. jsp执行过程 1)客户 ...

  9. Linux Mono Asp.net 部署方案

    1.Jexus 国内的 官网:http://www.jexus.org 2.Apache 官网:http://mono-project.com/Mod_mono 3.Nginx 官网:http://m ...

  10. 排列的Java递归语言实现

    在做算法题的时候,发现排列经常被使用到,是一个重要的知识点, 下面是博主修改过的代码,初学者,如有不足,欢迎指出 import java.util.ArrayList; import java.uti ...