Java程序中两种配置文件(xml和properties)的加载读取方法
Java程序中,经常需要从配置文件中加载并读取设置,以支持不同的配置环境和参数。最常用的配置文件格式是XML和properties。两种方法都非常基础,适合于简单的配置文件读取需求。对于更复杂的需求,可能需要更高级的解析技术或第三方库。
参考文档:
一、xml配置文件
<?xmlversion="1.0"encoding="UTF-8"?> <!-- const.xml --> <config> <database> <url>127.0.0.1</url> <port>1521</port> <login>admin</login> <password>pass</password> </database> </config>
1、Apache Commons Configuration读取xml配置文件
1)使用Maven引入Commons Configuration,pom.xml配置文件如下,
<dependencies> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>commons-jxpath</groupId> <artifactId>commons-jxpath</artifactId> <version>1.3</version> </dependency> </dependencies>
2)读取xml配置文件代码
XMLConfiguration config =new XMLConfiguration("const.xml"); //127.0.0.1 config.getString("database.url"); //1521 config.getString("database.port");
3)通过XPath表达式读取xml配置文件代码
XMLConfiguration config =new XMLConfiguration("const.xml"); config.setExpressionEngine(new XPathExpressionEngine()); //127.0.0.1 config.getString("databases/database[name = 'dev']/url"); //192.23.44.100 config.getString("databases/database[name = 'production']/url");
文档:
2、JDOM读取xml配置文件
文档:
SAXBuilder parser = new SAXBuilder(); Document docConfig = parser.build("config.xml"); Element elConfig = docConfig.getRootElement(); String host = elConfig.getChildText("url");
二、properties配置文件
dbpassword=password database=localhost dbuser=levi
1)读取properties配置文件代码如下,
Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream("config.properties"); //加载properties文件 prop.load(input); //get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } }
2)从项目的classpath中读取properties配置文件
Properties prop = new Properties(); InputStream input = null; try { String filename = "config.properties"; input = test.class.getClassLoader().getResourceAsStream(filename); if (input == null) { System.out.println("Sorry, unable to find " + filename); return; } //从class文件路径加载properties文件 prop.load(input); //获取属性值并打印出来 System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } }
参考文档:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2023-04-10 Docker CLI docker compose build常用命令
2022-04-10 .NET(C#) 去除JSON字符串反序列化对象列表中重复对象