JAVA 使用properties配置文件

来源:互联网 发布:如月疑云 知乎 编辑:程序博客网 时间:2024/06/06 02:10

在工程中,创建一个配置文件,例如,命名为 database.properties 注意后缀为properties 千万不要写成propertise,很像对不对

将文件放在src/main/resources下

文件内容

###############################数据库IP及端口信息##############################mysql.driver=com.mysql.jdbc.Drivermysql.jndi=jdbc:mysql://mysql.ip=192.168.1.1mysql.port=3388###############################数据库的数据库名##############################mysql.dbname=qatest###############################数据库的登录名及密码##############################mysql.username=qatestmysql.password=qatest###############################数据库的辅助设置#包括编码级别等信息##############################mysql.useUnicode=truemysql.characterEncoding=utf-8


然后就可以读取文件,获取配置信息,废话不说,直接代码

import java.util.Properties;import java.util.HashMap;import java.util.Enumeration;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import org.apache.log4j.Logger;import com.qa.ballon.api.util.U_Customed_Exception;public class U_File_Properties {private static Logger logger = Logger.getLogger(U_File_Properties.class);public static HashMap<String,String> dbproperties = new HashMap<String, String>();//用于获取数据库配置文件的各项信息public void M_get_db_propertise(String propath,String dbtype) throws U_Customed_Exception{Properties dbprop = new Properties();try{InputStream ins = getClass().getResourceAsStream(propath);dbprop.load(ins);logger.info("the database.propertise has got successfully!!");}catch(Exception e){logger.error("the database.propertise has some problems!!");e.printStackTrace();}if(dbtype.equals("mysql")){try {dbproperties.put("mysql.ip", dbprop.getProperty("mysql.ip"));dbproperties.put("mysql.port", dbprop.getProperty("mysql.port"));dbproperties.put("mysql.dbname", dbprop.getProperty("mysql.dbname"));dbproperties.put("mysql.username", dbprop.getProperty("mysql.username"));dbproperties.put("mysql.password", dbprop.getProperty("mysql.password"));dbproperties.put("mysql.useUnicode", dbprop.getProperty("mysql.useUnicode"));dbproperties.put("mysql.characterEncoding", dbprop.getProperty("mysql.characterEncoding"));logger.info("the database info has got successfully!!");} catch (Exception e) {// TODO: handle exceptionlogger.error("Failed to collect database info,please check the keyword and values!!");e.printStackTrace();}}else {logger.error("no such type of database then~~~ dbtype you gived is "+dbtype);throw new U_Customed_Exception("no such type of database!! the type of database you gived is "+dbtype);}}public static void main(String[] args) throws Exception{U_File_Properties ufp = new U_File_Properties();System.out.println(System.getProperty("user.dir"));ufp.M_get_db_propertise("/database.properties","mysql");System.out.println(ufp.dbproperties);}}

输出结果如下:

</pre><pre name="code" class="vb">2015-11-17 18:06:11,146  INFO main (com.qa.ballon.api.util.U_File_Properties:28)  - the database.propertise has got successfully!!
 2015-11-17 18:06:11,146  INFO main (com.qa.ballon.api.util.U_File_Properties:43)  - the database info has got successfully!!
 {mysql.password=qatest, mysql.username=qatest, mysql.characterEncoding=utf-8, mysql.useUnicode=true, mysql.dbname=qatest, mysql.port=3388, mysql.ip=192.168.1.1}


通过迭代的方式可以获取到properties中的全部内容

//获取properties全部信息    public static void getProperties(String filePath) {     Properties props = new Properties();        try {         InputStream ins = new BufferedInputStream (new FileInputStream(filePath));         props.load(ins);            Enumeration propen = props.propertyNames();             while (propen.hasMoreElements()) {              String propkey = (String) propen.nextElement();                    String propekeyval = props.getProperty (propkey);                    System.out.println(propkey+propkeyval);                }        } catch (Exception e) {         e.printStackTrace();        }    }





0 0
原创粉丝点击