简单读取dataSource.properties配置文件

package common.newEntity;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
/**
 * ********************************************************
 * @ClassName: newEntity
 * @Description: 
 * @author DoDo
 * @date 2017-12-27 下午06:29:37
 *******************************************************
 */
public class newEntity {
     //读取配置文件
     public static void main(String[] args) throws Exception { 
            //读取方式一
            // ClassPathResource的作用,如果没有指定相对的类名,该类将从类的根路径开始寻找某个 resource,如果指定了相对的类名,则根据指定类的相对路径来查找某个resource
            // ClassPathResource cr = new ClassPathResource("com.md.util/dataSource.properties");//会重新加载spring框架
            ClassPathResource cr = new ClassPathResource("dataSource.properties");
            // properties是配置文件,主要的作用是通过修改配置文件可以方便的修改代码中的参数,实现不用改class文件即可灵活变更参数。(方便使用)
            Properties pros = new Properties();
            try {
                //Properties的load方法其实就是传进去一个输入流,字节流或者字符流,字节流利用InputStreamReader转化为字符流,
                //然后字符流用BufferedReader包装,BufferedReader读取properties配置文件
                pros.load(cr.getInputStream());
                System.out.println(pros.getProperty("driver")); 
            } catch (IOException ex) {
                System.out.println("数据库连接文件不存在");
            }
            //读取方式二
            Properties properties=new Properties();  
            properties.load(new FileInputStream("E:/dataSource.properties"));  
            System.out.println(properties.getProperty("driver"));  
        }  
}

输出:
com.mysql.jdbc.Driver
com.mysql.jdbc.Driver

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值