Properties 配置文件
第一波:
在src文件下new一个file,命名为XXX.properties
Java中的一些键值对的配置文件专门提供这样的类和文件名
Properties 文件名后缀
Properties读取Properties文件的类
第二波:
读取配置配置文件:
1. 将配置文件变为流
2. 用Java提供的Properties类去加载这个流。
3. 调用Properties对象的getPropety的方法这个方法需要一个参数 键名返回的是一个值(字符串型)
<?xml version="1.0" encoding="utf-8" ?> <c3p0-config> <named-config name="mysql"> <property name="user">root</property> <property name="password">123</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/root</property> </named-config> </c3p0-config>
测试:
public class Dome { public static void main(String[] args) throws IOException { //Dome.class.getClassLoader()获取文件路径类加载器 InputStream in = Dome.class.getClassLoader().getResourceAsStream("datebase.properties"); //读取文件 Properties properties=new Properties(); properties.load(in); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String driverClass = properties.getProperty("driverClass"); String jdbcUrl= properties.getProperty("jdbcUrl"); System.out.println(user); System.out.println(password); System.out.println(driverClass); System.out.println(jdbcUrl); } }4.