简单读取dataSource.properties配置文件

来源:互联网 发布:淘宝信誉提升 编辑:程序博客网 时间:2024/05/01 10:53
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

2 0
原创粉丝点击