使用org.apache.commons.configuration2中提供的工具来读取属性文件
1.创建java工程
2.引入所需jar包
3.在src下创建属性文件properties.properties,内容如下:
username=eclipse
password=123456
4.创建工具类PropsUtils
public class PropertyUtils {
public static void main(String[] args) throws FileNotFoundException, ConfigurationException, IOException {
String relativelyPath = System.getProperty("user.dir");//获取当前项目的根目录
PropertiesConfiguration config = new PropertiesConfiguration();
config.read(new FileReader(relativelyPath + "/src/properties.properties"));
String username = config.getString("username");
System.out.println(username);
String password = config.getString("password");
System.out.println(password);
}
}
最后打印输出:
eclipse
123456
其他就不一一列举了,方法都差不多!
本文介绍如何利用Apache Commons Configuration API读取Java项目的属性文件。通过创建Java工程并引入必要的依赖,演示了如何从properties文件中加载配置,并通过代码示例展示了具体的实现过程。
1万+

被折叠的 条评论
为什么被折叠?



