方法太多,只写一种比较简单的。
,文件test1.properties内容
test1=123; test2=3211 属性道具=new属性(); 道具。负载(新FileInputStream (" src/test1.properties ")); System.out.println (prop.get (test1));
输出
<代码> 123;1 代码>
简单封装一下,完整代码
包propertis.test; 进口java.io.FileInputStream; 进口java.io.FileNotFoundException; 进口java.io.IOException; 进口java.util.Properties; 公共类测试{/* * * @param arg游戏 * @throws IOException * @throws FileNotFoundException */公共静态void main (String [] args)抛出FileNotFoundException, IOException {//TODO自动生成方法存根 属性道具=new属性(); 道具。负载(新FileInputStream (" src/test1.properties ")); System.out.println (prop.get (test1)); System.out.println (ProUtil.getTest1Value (test1)); System.out.println (ProUtil.getTest1Value (test2)); } } 类ProUtil { 私有静态属性道具=新属性(); 静态{ 尝试{ 道具。负载(新FileInputStream (" src/test1.properties ")); }捕捉(FileNotFoundException e) {//TODO自动生成的catch块 e.printStackTrace (); }捕捉(IOException e) {//TODO自动生成的catch块 e.printStackTrace (); } } 公共静态对象getTest1Value (String键){ 返回prop.get(关键); } }
输出
引用>
123; 123;
321年
方法:
属性=new属性(); FileInputStream=新FileInputStream (“* * . properties”); properties.load(的);in.close ();配置文件:
司机=com.mysql.jdbc.Driver url=jdbc: mysql://localhost: 3306/测试# 63;useUnicode=true& characterEncoding=utf - 8 用户名=根 密码=代码实现:
进口java.io.FileInputStream; 进口java.util.Properties; 公开课PropertiesTest { 私有静态最终字符串PROPERTIES_NAME=癲b.properties”; 公共静态字符串DB_DRIVER=零; 公共静态字符串DB_URL=零; 公共静态字符串DB_USER=零; 公共静态字符串DB_PWD=零; 静态{ FileInputStream=零; 尝试{ 属性=new属性(); 在新FileInputStream=(PROPERTIES_NAME); properties.load(的); DB_DRIVER=properties.getProperty(“司机”); DB_URL=properties.getProperty (“url”); DB_USER=properties.getProperty(“用户名”); DB_PWD=properties.getProperty (“passworld”); System.out.println(“读取配置信息成功!”); showConfig (); }捕捉(异常e) { e.printStackTrace (); System.out.println(“读取配置信息失败!”); 最后}{ 如果(在!=null) { 尝试{ in.close (); }捕捉(异常e) { e.printStackTrace (); } } } } 私有静态孔隙showConfig () { System.out.println(“- - - - - - - - - - - - - - - - - - - - - - - -配置信息- - - - - - - - - - - - - - - - - -”); system . out。println(“司机:“+ DB_DRIVER); system . out。println (“url:“+ DB_URL); system . out。println(“用户:”+ DB_USER); system . out。println (“passworld:“+ DB_PWD); System.out.println (“- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -”); } 公共静态void main (String [] args) { } }运行结果:
读取配置信息成功!
- - - - - - - - - - - - - - - - - - - - - - - -配置信息- - - - - - - - - - - - - - - - - -
引用>
司机:com.mysql.jdbc。司机
url: jdbc: mysql://localhost: 3306/测试# 63;useUnicode=true& characterEncoding utf - 8=
用户:根
passworld:空
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
以上所述是小编给大家介绍的Java读取,获取配置文件. properties中的数据,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
Java读取,获取配置文件. properties中的数据