读取Properties文件操作

import java.io.InputStream;
import java.util.Properties;
/**
 * 读取system.properties配置文件中的参数
 */
public class TestProperties {
    private static Properties TestProperties;
    //加载system.properties配置文件,读取文件的配置参数
    private static synchronized void loadProperties() {
        if(null==TestProperties) {
            try {
                Properties properties=new Properties();
                InputStream inputStream=TestProperties.class.getClassLoader().getResourceAsStream("system.properties");
                properties.load(inputStream);
                TestProperties=properties;
            }catch(Exception e) {
                throw new RuntimeException("未找到配置文件");
            }
        }
    }
    //读取配置文件中的参数的值
    public void get() {
      loadProperties();
      System.out.println(TestProperties.getProperty("jdbc.driverUrl"));
      System.out.println(TestProperties.getProperty("jdbc.driverClassName"));
      System.out.println(TestProperties.getProperty("jdbc.password"));
      System.out.println(TestProperties.getProperty("jdbc.user"));
    }
    public static void main(String[] args) {
        TestProperties testProperties=new TestProperties();
        testProperties.get();
    }
}

 

 

 

jdbc.driverUrl=jdbc:mysql://localhost:3306/idcard?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.password=root
jdbc.user=root
posted @ 2020-01-18 15:51  薄眠抛却陈年事。  阅读(131)  评论(0编辑  收藏  举报