getClassLoader().getResource() / class.getResource()获取文件内容

Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题。

获取方式有4种(类/类对象是否调取ClassLoader)
1.通过类.class.getResource()
2.通过类.class.getClassLoader().getResource()
3.通过类对象.getClass().getClassLoader()
4.通过类对象.getClass().getResource()

文件在工程中的位置这里介绍3种
在这里插入图片描述
1.src下
2.类所在目录下(同级目录下)
3.其他目录下

这样和上面的2中获取方式就组成了12种方法

类.class.getResource()类.class.getClassLoader().getResource()类对象.getClass().getClassLoader()类对象.getClass().getResource()
src下
同级目录下
其他目录下

1.Class.getResource(String path)

path不以’/'开头时,默认是从此类所在的包下取资源;
path 以’/'开头时,则是从ClassPath根下获取;

System.out.println(JDBCUtil.class.getResource(""));
System.out.println(JDBCUtil.class.getResource("/"));

结果
在这里插入图片描述

2.classLoader().getResource()

path不能以’/'开头;
path是从ClassPath根下获取;

System.out.println(JDBCUtil.class.getClassLoader().getResource(""));
System.out.println(JDBCUtil.class.getClassLoader().getResource("/"));//null

结果
在这里插入图片描述
上述的12种方法获取资源方式:

package JDBC;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

public class JDBCUtil {
    // 通过类.class.getClassLoader()的方式
    //src下
    public Properties getResSrcLoader() throws IOException {
        Properties pro = new Properties();
        URL res = JDBCUtil.class.getClassLoader().getResource("2.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }
    // 通过类.class.getClassLoader()的方式
    //同级目录下
    public Properties getResFileLoader() throws IOException {
        Properties pro = new Properties();
        URL res = JDBCUtil.class.getClassLoader().getResource("JDBC/1.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }
    // 通过类.class.getClassLoader()的方式
    // 其他目录下
    public Properties getResOtherFileLoader() throws IOException {
        Properties pro = new Properties();
        URL res = JDBCUtil.class.getClassLoader().getResource("day01/3.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }

    // 通过类.class.getResource的方式
    // src下
    public Properties getResSrc() throws IOException {
        Properties pro = new Properties();
        URL res = JDBCUtil.class.getResource("/2.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }

    // 通过类.class.getResource的方式
    // 同级目录下
    public Properties getResFile() throws IOException {
        Properties pro = new Properties();
        URL res = JDBCUtil.class.getResource("1.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }
    //通过类.class.getResource的方式
    // 其他目录下
    public Properties getResOtherFile() throws IOException {
        Properties pro = new Properties();
        URL res = JDBCUtil.class.getResource("/day01/3.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }






    // 通过对象.getClass().getClassLoader().getResource的方式
    //src下
    public Properties getResSrcLoader(JDBCUtil utils) throws IOException {
        Properties pro = new Properties();
        URL res = utils.getClass().getClassLoader().getResource("2.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }
    // 通过对象.getClass().getClassLoader().getResource的方式
    //同级目录下
    public Properties getResFileLoader(JDBCUtil utils) throws IOException {
        Properties pro = new Properties();
        URL res = utils.getClass().getClassLoader().getResource("JDBC/1.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }
    // 通过对象.getClass().getClassLoader().getResource的方式
    // 其他目录下
    public Properties getResOtherFileLoader(JDBCUtil utils) throws IOException {
        Properties pro = new Properties();
        URL res = utils.getClass().getClassLoader().getResource("day01/3.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }

    // 通过对象.getClass().getResource的方式
    // src下
    public Properties getResSrc(JDBCUtil utils) throws IOException {
        Properties pro = new Properties();
        URL res = utils.getClass().getResource("/2.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }

    // 通过类.class.getResource的方式
    // 同级目录下
    public Properties getResFile(JDBCUtil utils) throws IOException {
        Properties pro = new Properties();
        URL res = utils.getClass().getResource("1.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }
    //通过类.class.getResource的方式
    // 其他目录下
    public Properties getResOtherFile(JDBCUtil utils) throws IOException {
        Properties pro = new Properties();
        URL res = utils.getClass().getResource("/day01/3.properties");
        String path = res.getPath();
        pro.load(new FileReader(path));
        return pro;
    }


    public static void main(String[] args) throws IOException {
        JDBCUtil utils = new JDBCUtil();
        System.out.println(utils.getResSrcLoader());
        System.out.println(utils.getResFileLoader());
        System.out.println(utils.getResOtherFileLoader());
        System.out.println(utils.getResSrc());
        System.out.println(utils.getResFile());
        System.out.println(utils.getResOtherFile());

        System.out.println(utils.getResSrcLoader(utils));
        System.out.println(utils.getResFileLoader(utils));
        System.out.println(utils.getResOtherFileLoader(utils));
        System.out.println(utils.getResSrc(utils));
        System.out.println(utils.getResFile(utils));
        System.out.println(utils.getResOtherFile(utils));
    }
}

在这里插入图片描述

总结

getResource() 不加/表示当前目录下,加/表示当前项目根目录下
getClassLoader().getResource()不加/表示当前项目根目录下,加/为null

基于上述目录结构:

类.class.getResource()类.class.getClassLoader().getResource()类对象.getClass().getResource()类对象.getClass().getClassLoader()
src下/文件文件/文件文件
同级目录下文件目录/文件文件目录/文件
其他目录下/其他目录/文件其他目录/文件/其他目录/文件其他目录/文件
  • 9
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@nanami

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值