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”);