首页 > 其他 > 详细

读取Properties文件操作

时间:2020-01-18 16:16:25      阅读:76      评论:0      收藏:0      [点我收藏+]
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

读取Properties文件操作

原文:https://www.cnblogs.com/byczyz/p/12209216.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!