工程目录结构:

prj(工程根目录)

  cn

    json

      classloader

        GetResourceByClassAndClassLoader.Java

  beans.xml

  

/**
*
*/
package cn.json.classloader; import java.io.InputStream; /**
* @author json
*
* @date 2014-5-7
*
* @version 1.0
*/
public class GetResourceByClassAndClassLoader { /**
* class 获取资源是相对于当前class所在路径去获取
*
* classloader 是相对于classpath去获取相应的资源,采用绝对路径
*
* @param args
*/
public static void main(String[] args) {
GetResourceByClassAndClassLoader bean = new GetResourceByClassAndClassLoader();
InputStream is = bean.getClass().getResourceAsStream("../../../beans.xml");
if (is == null) {
System.out.println("resources not found!");
}
is = null;
is = bean.getClass().getResourceAsStream("/beans.xml");
if (is == null) {
System.out.println("resources not found!");
}
is = null;
is = bean.getClass().getClassLoader().getResourceAsStream("beans.xml");
if (is == null) {
System.out.println("resources not found!");
}
}
}

class getResourceAsStream 和 classloader getResourceAsStream获取资源的不同的更多相关文章

  1. Class.getResourceAsStream和ClassLoader.getResourceAsStream方法

    项目中,有时候要读取当前classpath下的一些配置文件,下面介绍下Class.getResourceAsStream和ClassLoader.getResourceAsStream两种方法以及两者 ...

  2. Class.getResourceAsStream()与ClassLoader.getResourceAsStream()获取资源时的路径说明

    Class.getResourceAsStream(): com.xusweeter.iot.ws.vodafone.config.VodafoneServiceConfig.class.getRes ...

  3. 对Class.getResourceAsStream和ClassLoader.getResourceAsStream方法所使用的资源路径的解释

    这是个非常基础的问题了,这里提供一些演示样例,帮助高速理解和记忆这个问题. 在该方法的文档:http://docs.oracle.com/javase/7/docs/api/java/lang/Cla ...

  4. Class.getResourceAsStream()与ClassLoader.getResourceAsStream()的区别

    Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致. 例如你写了一个MyTest类在包com.test.mycode 下,那么MyTest.clas ...

  5. Class.getResourceAsStream() VS. ClassLoader.getResourceAsStream()

    For Class.getResourceAsStream(String name), if the name parameter doesn't start with a "/" ...

  6. JavaWeb基础: 获取资源文件

    Web工程在编译构建完毕以后,需要部署到Tomcat上运行,资源的硬盘路径也会随着改变.要想对资源文件进行读写操作需要获取其硬盘地址,在Web工程中通常通过ServletContext/ClassLo ...

  7. Java 使用getClass().getResourceAsStream()方法获取资源

    之前想获取一个资源文件做一些处理,使用getClass().getResourceAsStream()一直拿不到文件. 具体的用法. 1 InputStream is = this.getClass( ...

  8. ClassLoader.getResourceAsStream(name);获取配置文件的方法

    ClassLoader.getResourceAsStream(name);路径问题 InputStream in = getClass().getResourceAsStream('/'+" ...

  9. JAVA 笔记 ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别

       Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致. 例如你写了一个MyTest类在包com.test.mycode 下,那么MyTest.c ...

随机推荐

  1. zoj 1729 Hidden Password

    Hidden Passwordhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=729 Time Limit: 2 Seconds ...

  2. HDU1164

    //HDU 1164 //输入一个数(1<x<=65535) 转化为素数的乘积() #include "iostream" #include "cstdio& ...

  3. POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解

    Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...

  4. Create MSSQL Procedure

    代码: CREATE PROCEDURE [dbo].[sp_UpdateCouponCount] AS GO

  5. mixin模式特点

    mixin模式特点: 1.单一功能, 2.不和基类关联,可以和任意基类组合,基类可以不和mixin关联就可以初始化成功 3.不使用 super() 用法

  6. 好用的python库(转)

    这个专区就是用来分享你在使用 Python 过程中发现的或者自己写的 Python 库.有时候一个好用的库将大大节省一个开发者的时间,也会让开发者多这个库的作者感激涕零的.例如我在开发 Pythonz ...

  7. Druid连接池及监控在spring中的配置

    Druid连接池及监控在spring配置如下: <bean id="dataSource" class="com.alibaba.druid.pool.DruidD ...

  8. Anniversary party(树上dp+HDU1520)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目: 题意:一个学校要办校庆,校长决定邀请员工参加,但是下属和他的直系同时参加的话,下属将会无 ...

  9. poj 1797

    2013-09-08 09:48 最大生成树,输出生成树中最短的边儿即可 或者对边儿排序,二份答案+BFS判断是否1连通N 时间复杂度都是O(NlogN)的 附最大生成树pascal代码 //By B ...

  10. Python3 xml模块的增删改查

    xml数据示例 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <data>     < ...