文件绝对路径获取问题

通常都是  FileReader reader = new FileReader("classToGet.properties"); 获取路径问题,但是这种方式的路径的缺陷:可移植性差,离开idea换到其他位置,这个路径就会失效。

1、比较通用的一种路径获取

不过有个前提:这个文件必须在类路径下(就是在src下的都是类路径)

代码如下:

public class reflect_path {
    public static void main(String[] args) throws FileNotFoundException {
        //这个是在src的bean目录下的
        String path = Thread.currentThread().getContextClassLoader().getResource("cn/com/Ycy/Beans/name.properties").getPath();
        System.out.println(path);
        String path2 = Thread.currentThread().getContextClassLoader().getResource("classToGet.properties").getPath();
        System.out.println(path2);
    }
}

输出结果:

/D:/ideaProject/TCP/InetAddress/out/production/InetAddress/cn/com/Ycy/Beans/name.properties
/D:/ideaProject/TCP/InetAddress/out/production/InetAddress/classToGet.properties

这样我们就可以如下写创建对象:

public class test03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        String path = Thread.currentThread().getContextClassLoader().getResource("classToGet.properties").getPath();
        FileReader reader = new FileReader(path);
        //创建属性类对象map
        Properties pro = new Properties();
        //加载
        pro.load(reader);
        //关闭流
        reader.close();
        String className = pro.getProperty("className");
        Class c = Class.forName(className);
        Object user = c.newInstance();
        System.out.println(user);
    }
}

2、以流的形式直接返回

可以在简化代码的形式,不获取获取xxx.properties文件的path,直接返回流的形式,我们不用再使用FileReader了

public class IO_return {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        InputStream reader = Thread.currentThread().getContextClassLoader().getResourceAsStream("classToGet.properties");
        //创建属性类对象map
        Properties properties = new Properties();
        //加载
        properties.load(reader);
        reader.close();
        Class c = Class.forName(properties.getProperty("className"));
        Object obj = c.newInstance();
        System.out.println(obj);
    }
}

3、资源绑定器(最简便的方式)

对,前面都白雪了,哈哈哈,感觉被骗了,有这么好用的。

java.util.ResourceBundle
       这个类是专门用来获取属性配置文件的内容

只能读取“xxx.properties”属性配置文件,且该文件一定得放在src类路径下

注意:这个类只能读取“xxx.properties”属性配置文件,且该文件一定得放在src类路径下。

代码演示:

public class ResourceBundleTest {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //这个name.properties是在Beans目录下的
        ResourceBundle bundle = ResourceBundle.getBundle("cn/com/Ycy/Beans/name");
        String className = bundle.getString("className");
        Class c  = Class.forName(className);
        Object obj = c.newInstance();
        System.out.println(obj);


        ResourceBundle bundle2 = ResourceBundle.getBundle("classToGet");
        String className2 = bundle2.getString("className");
        Class c2  = Class.forName(className2);
        Object obj2 = c2.newInstance();
        System.out.println(obj2);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43725517/article/details/108300774