您的位置 首页 java

java中的Properties配置文件的用法

Java 中Properties的用法

1、新建properties文件

在src文件下new一个file,命名为XXX.properties

2、编写配置文件

jdbc .driver=com.mysql.jdbc.Driver

XXX.jdbc.url=jdbc:mysql:// 127.0.0.1 :3306/basedb?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&autoReconnect=true

XXX.jdbc.username=xxxx

XXX.jdbc.password=xxxxxx

1、=两边别打空格

2、名值对写完别打分号

3、在Java中调用配置文件

1、实例化Properties类(Java.util.Properties)

Properties p = new Properties();

2、利用Properties中的load()方法加载配置文件。

这里要注意的是使用类的反射加载配置文件,并且要写入配置文件的正确路径

InputStream in =xxx.class.getClassLoader().getResourceAsStream(“XXX.properties”);

//InputStream in = new BufferedInputStream(new FileInputStream(filePath));

props.load(in);

String name = props.getProperty(“name”);

String age = props.getProperty(“age”);

String sex = props.getProperty(“sex”);

文章来源:智云一二三科技

文章标题:java中的Properties配置文件的用法

文章地址:https://www.zhihuclub.com/173959.shtml

关于作者: 智云科技

热门文章

网站地图