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