在java中的API里,有两种方式来使用classpath读取资源。

1. Class的getResource()

2. ClassLoader的getResource()

但是两者有一定区别,运行以下程序:

package zero.xml.config;

public class Main {

    public static void main(String[] args) {
new Main().testGetResource();
} public void testGetResource() { System.out.println(Main.class.getResource("/").getPath());
System.out.println(Main.class.getResource("/app.properties").getPath());
System.out.println(Main.class.getResource("").getPath());
System.out.println(Main.class.getResource("app.properties").getPath());
System.out.println("-------------------");
System.out.println(this.getClass().getResource("/").getPath());
System.out.println(this.getClass().getResource("/app.properties").getPath());
System.out.println(this.getClass().getResource("").getPath());
System.out.println(this.getClass().getResource("app.properties").getPath());
System.out.println("-------------------");
System.out.println(Main.class.getClassLoader().getResource("").getPath());
System.out.println(Main.class.getClassLoader().getResource("app.properties").getPath());
System.out.println(Main.class.getClassLoader().getResource("zero/xml/config").getPath());
System.out.println(Main.class.getClassLoader().getResource("zero/xml/config/app.properties").getPath());
}
}

得到输出为:

/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config/
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
-------------------
/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config/
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
-------------------
/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties

也就是:

1. 如果想获得classpath,使用以下方法:

System.out.println(Main.class.getResource("/").getPath());

System.out.println(Main.class.getClassLoader().getResource("").getPath());

2. 如果想获得classpath下的文件,使用以下方法:

System.out.println(Main.class.getResource("/app.properties").getPath());

System.out.println(Main.class.getClassLoader().getResource("app.properties").getPath());

3. 如果想获得当前类(比如zero.xml.config.Main)的路径,使用以下方法:

System.out.println(Main.class.getResource("").getPath());

System.out.println(Main.class.getClassLoader().getResource("zero/xml/config").getPath());

4. 如果想获得当前类路径下的文件,使用以下方法:

System.out.println(Main.class.getResource("app.properties").getPath());

System.out.println(Main.class.getClassLoader().getResource("zero/xml/config/app.properties").getPath());

注意,如果获取的文件或路径不存在,getResource()会返回null。比如,getClassLoader().getResource("/")就会返回null。

classpath获取--getResource()的更多相关文章

  1. classpath: VS classpath*:

    同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源 classpath*:需要遍历所有的 ...

  2. classpath和filepath

    ******************************** java中的相对路径和绝对路径 ******************************** 相对路径(其实就是编译后的路径) - ...

  3. classpath: 和classpath*:的区别

    classpath本质是jvm的根路径,jvm获取资源都是从该根路径下找的,注意这个根路径是个逻辑路径,并不是磁盘路径.比如两个jar包的路径是/a/a.jar和/b/b.jar,但是用classpa ...

  4. 通过SVN获取变更列表,得到对应的最新class

    通过本地SVN获得未提交的文件列表获取工程中最新的class的方式参考: 增量部署代码利用批处理命令按原始结构复制指定的文件 新写了一个增强版,根据已提交至SVN的代码loglist,获取最新的cla ...

  5. [Java拾遗三]JavaWeb基础之Servlet

    Servlet    1,servlet介绍        servlet是一项动态web资源开发技术.        运行在服务器端.        作用:处理业务逻辑,生成动态的内容,返回给浏览器 ...

  6. 上次遗留下来的XMLUtil的问题

    ·在上周留下了一个关于XMLUtil的问题,问题大概是这样的,需要通过读取一个XML文件,然后在内存中生成一个对应的javaBean.之前写的那个很是糟糕,照着一个XML去写了一个"Util ...

  7. 从源码角度深入分析log4j配置文件使用

    log4j在日常开发中经常使用,但有时候对 配置文件应该放到什么位置有疑惑.现在我们通过从代码的角度来看待这个问题, 看完后你也许会恍然大悟哦. 开始吧. Log4j的组成及架构: Log4j由三个重 ...

  8. 从源码来理解slf4j的绑定,以及logback对配置文件的加载

    项目中的日志系统使用的是slf4j + logback.slf4j作为一个简单日志门面,为各种loging APIs(像java.util.logging, logback, log4j)提供一个简单 ...

  9. spring资源访问接口和资源加载接口

    spring 资源访问接口 JDK提供的资源访问类,如java.net.URL.File等,不能很好地满足各种资源的访问需求,比如缺少从类路径或者Web容器的上下文中获取资源的操作类. 鉴于此,spr ...

随机推荐

  1. meta小解

    meta是html中的一个辅助标签,位于<head>与<title>之间,它能提供用户不可见的信息,数据结构为键值对 meta标签格式<meta http-equiv/n ...

  2. Spring MVC如何进行JSON数据的传输与接受

    本篇文章写给刚接触SpingMVC的同道中人,虽然笔者本身水平也不高,但聊胜于无吧,希望可以给某些人带来帮助 笔者同时再次说明,运行本例时,需注意一些配置文件和网页脚本的路径,因为笔者的文件路径与读者 ...

  3. 分布式版本控制系统Git-----6.Git 常见命令一览表

    说明/备注 命令 备注 保存更新 git add [-i] -i 逐个确认 检查更新 git status 提交更新 git commit [-a] -m "<更新说明>&quo ...

  4. CodeForces 705B Spider Man

    水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...

  5. vb.net_一个半成品

    Imports System.Text Imports System.Runtime.InteropServices Public Class Form1 '引用win32api进行枚举窗体句柄操作 ...

  6. cobaltstrike3.5使用记录

    一.服务器搭建与连接(1)团队服务器上: sudo ./teamserver 服务器IP 连接密码 (VPS的话要写外网ip,并且可以进行端口映射,默认使用50050端口) 但是这个一关闭团队服务器也 ...

  7. 第九十九节,JavaScript数据类型

    JavaScript数据类型 学习要点: 1.typeof操作符 2.Undefined类型 3.Null类型 4.Boolean类型 5.Number类型 6.String类型 7.Object类型 ...

  8. mysql表设计

    model表 记录网站模块:如视频,音频,调查,01发布内容时.可以指定发布到哪个模块下02可以统计每个模块的访问量设计表注意点01主键不要用id (全部使用 当前表名+id 如modelid)02n ...

  9. Spring入门学习(二)三种实例化bean的方法

    前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...

  10. CodeForces 701C They Are Everywhere 尺取法

    简单的尺取法…… 先找到右边界 然后在已经有了所有字母后减小左边界…… 不断优化最短区间就好了~ #include<stdio.h> #include<string.h> #d ...