java读取propertiies文件例子

这里写图片描述

jdbc.properties的内容
username=root
password=123456
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ttms?useUnicode=true&characterEncoding=utf-8
下边我删掉了一些

测试用例如下

使用java.util.Properties类的load()方法

/** 
* @ClassName: PropertiesTest 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author: maqiang 13032946420@163.com  
* @date 2018年5月12日 上午10:19:08 
*  
*/

class PropertiesTest {

    private final String dbConnFile = "src/main/resources/jdbc.properties";
    @Test
    public void loadConnProPerties() throws Exception, IOException {


        /*1。使用java.util.Properties类的load()方法
        示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
        API: load(InputStream) / store(OutputStream, String)方法  


        Properties p = new Properties();
        p.load(in);*/
        Properties props= new Properties();
        props.load(new FileInputStream(dbConnFile));
        System.out.println(props);
        System.out.println(props.getProperty("driver"));            
    }
}

测试结果:

{url=jdbc:mysql://localhost:3306/ttms?useUnicode=true&characterEncoding=utf-8, druid.pool.size.min=3, jdbc.password=123456, username=root, druid.pool.size.max=20, jdbc.url=jdbc:mysql://localhost:3306/ttms?useUnicode=true&characterEncoding=utf-8, jdbc.driverClassName=com.mysql.jdbc.Driver, druid.pool.size.init=3, jdbc.username=root, password=123456, driver=com.mysql.jdbc.Driver}
com.mysql.jdbc.Driver

等价代码
class PropertiesTest {

    private final String dbConnFile = "src/main/resources/jdbc.properties";
    @Test
    public void loadConnProPerties() throws Exception, IOException {


        /*1。使用java.util.Properties类的load()方法
        示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
        使用了转化流
        API: load(InputStream) / store(OutputStream, String)方法  


        Properties p = new Properties();
        p.load(in);*/
        Properties props= new Properties();
        props.load(new BufferedInputStream(new FileInputStream(dbConnFile)));
        System.out.println(props);
        System.out.println(props.getProperty("driver"));    

    }
}
使用class变量的getResourceAsStream()方法
class PropertiesTest {

    private final String dbConnFile = "src/main/resources/jdbc.properties";
    private final String dbConnFiletwo ="/main/resources/jdbc.properties";
    @Test
    public void loadConnProPerties() throws Exception, IOException {


        /*使用class变量的getResourceAsStream()方法
        示例: InputStream in = this.class.getResourceAsStream(name);
        Properties p = new Properties();
        p.load(in);*/

        Properties props= new Properties();

        InputStream inputStream =  PropertiesTest.class.getResourceAsStream(dbConnFiletwo);

        props.load(inputStream);


        System.out.println(props);
        System.out.println(props.getProperty("driver"));    

    }

}

运行结果如上
使用java.util.ResourceBundle类的getBundle()方法
class PropertiesTest {

    private final String dbConnFile = "src/main/resources/jdbc.properties";
    private final String dbConnFiletwo ="/main/resources/jdbc.properties";
    private final String dbConnFilethree ="main/resources/jdbc";
    @Test
    public void loadConnProPerties() throws Exception, IOException {


        /*
         * 使用java.util.ResourceBundle类的getBundle()方法
        示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
        */
        ResourceBundle rb = ResourceBundle.getBundle(dbConnFilethree);


        System.out.println(rb.getString("driver"));
        System.out.println(rb); 

    }
}
运行结果如上所示
使用class.getClassLoader()
class PropertiesTest {

    private final String dbConnFile = "src/main/resources/jdbc.properties";
    private final String dbConnFiletwo ="/main/resources/jdbc.properties";
    private final String dbConnFilethree ="main/resources/jdbc";
    private final String dbConnFilefour ="main/resources/jdbc.properties";
    @Test
    public void loadConnProPerties() throws Exception, IOException {


        /*
         *使使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
        */
        InputStream inStream = PropertiesTest.class.getClassLoader().getResourceAsStream(dbConnFilefour);
        Properties properties = new Properties();
        properties.load(inStream);

        System.out.println(properties.getProperty("driver"));
        System.out.println(properties);     
    }

}
使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
class PropertiesTest {

    private final String dbConnFile = "src/main/resources/jdbc.properties";
    private final String dbConnFiletwo ="/main/resources/jdbc.properties";
    private final String dbConnFilethree ="main/resources/jdbc";
    private final String dbConnFilefour ="main/resources/jdbc.properties";
    @Test
    public void loadConnProPerties() throws Exception, IOException {


        /*
         *使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
        */
        InputStream inStream =ClassLoader.getSystemResourceAsStream(dbConnFilefour);
        Properties properties = new Properties();
        properties.load(inStream);

        System.out.println(properties.getProperty("driver"));
        System.out.println(properties);     
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值