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
- JAVA 使用properties配置文件
- Java使用properties读取配置文件
- java中配置文件 .properties使用
- Java中封装配置文件(1)-使用Properties
- Java中封装配置文件(1)-使用Properties
- Java中封装配置文件(1)-使用Properties
- java中的配置文件(Properties)的使用
- Java中封装配置文件(1)-使用Properties
- Java中封装配置文件使用Properties
- 使用java读取配置文件(ini、properties)
- Java使用Properties类读取配置文件
- java使用Properties类生成配置文件
- 使用java.util.Properties类读写配置文件
- java使用Properties类读取配置文件信息
- JAVA——配置文件Properties的使用
- 问题:使用java中properties加载配置文件
- java中配置文件.properties的使用
- 配置文件properties的使用
- css样式中关于li的横向排列
- 实现滑动的七种方法
- 链表操作(1)
- css3照片墙
- Linux c 源码(getvalbypos根据位置和特定的字符解析所需的信息)
- JAVA 使用properties配置文件
- springmvc通过@ResponseBody 自动解析为json对象支持中文
- MPAndroidChart——饼图
- oc字符串的简单使用
- Caused by: java.lang.IllegalStateException: not connected to MediaScannerSer
- 回到首页及QQ客服 按钮悬浮实现
- 渲染流水线
- 【转】 cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- android LayoutInflater原理