当前位置 博文首页 > i_mycode的博客:getClassLoader().getResource() / class.getRe

    i_mycode的博客:getClassLoader().getResource() / class.getRe

    作者:[db:作者] 时间:2021-08-05 15:58

    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
    
    下一篇:没有了