Java 读取、获取配置文件.properties中的数据
java获取配置文件.properties中的数据,具体内容如下所示:
方法太多,只写一种比较简单的。
文件test1.properties内容
test1=123; test2=3211 Propertiesprop=newProperties(); prop.load(newFileInputStream("src/test1.properties")); System.out.println(prop.get("test1"));
输出
123;1
简单封装一下,完整代码
packagepropertis.test; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; importjava.util.Properties; publicclassTest{ /** *@paramargs *@throwsIOException *@throwsFileNotFoundException */ publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException{ //TODOAuto-generatedmethodstub Propertiesprop=newProperties(); prop.load(newFileInputStream("src/test1.properties")); System.out.println(prop.get("test1")); System.out.println(ProUtil.getTest1Value("test1")); System.out.println(ProUtil.getTest1Value("test2")); } } classProUtil{ privatestaticPropertiesprop=newProperties(); static{ try{ prop.load(newFileInputStream("src/test1.properties")); }catch(FileNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } publicstaticObjectgetTest1Value(Stringkey){ returnprop.get(key); } }
输出
123;
123;
321
下面看下Java读取Properties配置文件
方法:
Propertiesproperties=newProperties(); FileInputStreamin=newFileInputStream("**.properties"); properties.load(in); in.close();
配置文件:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 username=root password=
代码实现:
importjava.io.FileInputStream; importjava.util.Properties; publicclassPropertiesTest{ privatestaticfinalStringPROPERTIES_NAME="db.properties"; publicstaticStringDB_DRIVER=null; publicstaticStringDB_URL=null; publicstaticStringDB_USER=null; publicstaticStringDB_PWD=null; static{ FileInputStreamin=null; try{ Propertiesproperties=newProperties(); in=newFileInputStream(PROPERTIES_NAME); properties.load(in); DB_DRIVER=properties.getProperty("driver"); DB_URL=properties.getProperty("url"); DB_USER=properties.getProperty("username"); DB_PWD=properties.getProperty("passworld"); System.out.println("读取配置信息成功!"); showConfig(); }catch(Exceptione){ e.printStackTrace(); System.out.println("读取配置信息失败!"); }finally{ if(in!=null){ try{ in.close(); }catch(Exceptione){ e.printStackTrace(); } } } } privatestaticvoidshowConfig(){ System.out.println("-----------------------配置信息-----------------"); System.out.println("dirver:"+DB_DRIVER); System.out.println("url:"+DB_URL); System.out.println("user:"+DB_USER); System.out.println("passworld:"+DB_PWD); System.out.println("----------------------------------------------"); } publicstaticvoidmain(String[]args){ } }
运行结果:
读取配置信息成功!
-----------------------配置信息-----------------
dirver:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
user:root
passworld:null
----------------------------------------------
总结
以上所述是小编给大家介绍的Java读取、获取配置文件.properties中的数据,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!