Java配置文件详解:.properties的使用与操作

5星 · 超过95%的资源 需积分: 11 36 下载量 85 浏览量 更新于2024-10-26 2 收藏 7KB TXT 举报
Java的property配置文件是一种常用的软件配置管理方式,用于存储程序运行时可能需要改变的参数,以提高代码的可维护性和灵活性。当我们在开发过程中遇到需要频繁调整的配置,如数据库连接信息(如IP地址、数据库名称、用户名和密码)或系统设置时,不建议直接硬编码在代码中,以免未来需要修改时难以管理。Java提供了一种标准化的解决方案,那就是通过`.properties`文件来存储这些配置。 `.properties`文件以键值对的形式组织,每行代表一个配置项,键(key)和值(value)之间使用等号(`=`)隔开,通常键前会有一个井号(`#`)表示注释。例如: ``` dbPort=localhost databaseName=mydb dbUserName=root dbPassword=root dbTable=mytable ip=192.168.0.9 ``` Java中处理`.properties`文件的核心类是`java.util.Properties`。以下是一些关键的`Properties`类方法及其用途: 1. `getProperty(String key)`:这个方法用于根据指定的键获取其对应的值。如果键不存在,返回`null`。 2. `load(InputStream inStream)`:从输入流(如FileInputStream)加载`.properties`文件内容到Properties对象,方便后续操作。 3. `setProperty(String key, String value)`:设置指定键的值,如果键已存在则覆盖旧值。 4. `store(OutputStream outStream, String comments)`:将Properties对象中的内容以`.properties`格式保存到输出流,通常用于持久化存储。 5. `clear()`:清除Properties对象中的所有键值对,实现清空配置。 在实际应用中,我们可以创建一个简单的`Configuration`类来操作`.properties`文件,例如: ```java package configuration; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class Configuration { private Properties properties; public Configuration() throws IOException { properties = new Properties(); try (FileInputStream fis = new FileInputStream("test.properties")) { properties.load(fis); } } // 示例方法获取配置 public String getDbPort() { return properties.getProperty("dbPort"); } // 示例方法设置配置 public void setDbPort(String newPort) { properties.setProperty("dbPort", newPort); } // 将配置保存回文件 public void save() throws IOException { try (FileOutputStream fos = new FileOutputStream("test.properties")) { properties.store(fos, "Updated configuration"); } } public static void main(String[] args) throws FileNotFoundException, IOException { Configuration config = new Configuration(); String port = config.getDbPort(); config.setDbPort("new_ip_address"); config.save(); } } ``` 通过这种方式,当配置发生变化时,只需要修改`.properties`文件,无需重新编译代码。这大大提高了代码的可重用性和维护性。

java.lang.ExceptionInInitializerError at com.example.demo.MyBatisExampleDemoTest.test1(MyBatisExampleDemoTest.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55) Caused by: org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in SQL Mapper Configuration ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.datasource.DataSourceException: Unknown DataSource property: serverTimezone at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:52) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:36) at com.bosssoft.hr.train.j2se.mybatis.utils.MyBatisUtils.<clinit>(MyBatisUtils.java:27) ... 28 more Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.datasource.DataSourceException: Unknown DataSource property: serverTimezone at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:122) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:50) ... 30 more Caused by: org.apache.ibatis.datasource.DataSourceException: Unknown DataSource property: serverTimezone at org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory.setProperties(UnpooledDataSourceFactory.java:55) at org.apache.ibatis.builder.xml.XMLConfigBuilder.dataSourceElement(XMLConfigBuilder.java:329) at org.apache.ibatis.builder.xml.XMLConfigBuilder.environmentsElement(XMLConfigBuilder.java:283) at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:117) ... 32 more

2023-07-15 上传