Java 放置和读取Properties文件

导读:本篇文章讲解 Java 放置和读取Properties文件,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Java 放置和读取Properties文件

1、在你的module下右击->New->Directoey

在这里插入图片描述

2、将新建的目录命令为resources

在这里插入图片描述

3、右击resources目录点击Mark Directory as—->点击Resources Root

在这里插入图片描述

4、将Properties文件放入resources目录下

在这里插入图片描述

5、修改你的代码,读取Properties文件

通过以下代码格式读取Properties文件

prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("bean.properties"));
    /**
     * 读取配置文件中的类名创建对象
     */
    public static Object getInstance(){
        Object obj = null;
        try {
            // 读取配置文件中的类名
            Properties prop = new Properties();
//            prop.load(new FileReader("H:\\POWER_NODE\\Projects\\AfterClass\\ClassroomTeachingArrangement\\55反射\\src\\模拟案例\\创建任意类对象的工具类\\bean.properties"));
            prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("bean.properties"));
            String className = prop.getProperty("className");
            // 反射
            Class<?> clazz = Class.forName(className);
            Constructor<?> declaredConstructor = clazz.getDeclaredConstructor();
            declaredConstructor.setAccessible(true);
            obj = declaredConstructor.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return obj;
    }

6、后续项目模块会打成jar包

具体操作详见我的另一篇博客——通过IDEA打jar包
https://huanghaoheng.blog.csdn.net/article/details/126331829

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/85592.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!