Java读取.properties配置文件的几种方式
Java开发中,需要将一些易变的配置参数放置再XML配置文件或者properties配置文件中。然而XML配置文件需要通过DOM或SAX方式解析,而读取properties配置文件就比较容易。
介绍几种读取方式:
1、基于ClassLoder读取配置文件
注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。
Propertiesproperties=newProperties();
//使用ClassLoader加载properties配置文件生成对应的输入流
InputStreamin=PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties");
//使用properties对象加载输入流
properties.load(in);
//获取key对应的value值
properties.getProperty(Stringkey);
2、基于InputStream读取配置文件
注意:该方式的优点在于可以读取任意路径下的配置文件
Propertiesproperties=newProperties();
//使用InPutStream流读取properties文件
BufferedReaderbufferedReader=newBufferedReader(newFileReader("E:/config.properties"));
properties.load(bufferedReader);
//获取key对应的value值
properties.getProperty(Stringkey);
3、通过java.util.ResourceBundle类来读取,这种方式比使用Properties要方便一些
1>通过ResourceBundle.getBundle()静态方法来获取(ResourceBundle是一个抽象类),这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可
properties.getProperty(Stringkey);
//config为属性文件名,放在包com.test.config下,如果是放在src下,直接用config即可
ResourceBundleresource=ResourceBundle.getBundle("com/test/config/config");
Stringkey=resource.getString("keyWord");
2>从InputStream中读取,获取InputStream的方法和上面一样,不再赘述
ResourceBundleresource=newPropertyResourceBundle(inStream);
注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:config.properties入在com.test.config包下,则要使用com/test/config/config.properties(通过Properties来获取)或com/test/config/config(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用config.properties或config即可。
下面附上几种方式的测试代码,仅供参考:
packagecom.test.properties;
importjava.io.BufferedInputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Enumeration;
importjava.util.Properties;
importorg.springframework.core.io.support.PropertiesLoaderUtils;
/**
*
*@ClassName:TestProperties
*@Description:获取配置文件信息
*@date:2017年11月25日上午10:56:00
*@version:1.0.0
*/
publicclassTestProperties{
/**
*
*@Title:printAllProperty
*@Description:输出所有配置信息
*@paramprops
*@returnvoid
*@throws
*/
privatestaticvoidprintAllProperty(Propertiesprops)
{
@SuppressWarnings("rawtypes")
Enumerationen=props.propertyNames();
while(en.hasMoreElements())
{
Stringkey=(String)en.nextElement();
Stringvalue=props.getProperty(key);
System.out.println(key+":"+value);
}
}
/**
*根据key读取value
*
*@Title:getProperties_1
*@Description:第一种方式:根据文件名使用spring中的工具类进行解析
*filePath是相对路劲,文件需在classpath目录下
*比如:config.properties在包com.test.config下,
*路径就是com/test/config/config.properties
*
*@paramfilePath
*@paramkeyWord
*@return
*@returnString
*@throws
*/
publicstaticStringgetProperties_1(StringfilePath,StringkeyWord){
Propertiesprop=null;
Stringvalue=null;
try{
//通过Spring中的PropertiesLoaderUtils工具类进行获取
prop=PropertiesLoaderUtils.loadAllProperties(filePath);
//根据关键字查询相应的值
value=prop.getProperty(keyWord);
}catch(IOExceptione){
e.printStackTrace();
}
returnvalue;
}
/**
*读取配置文件所有信息
*
*@Title:getProperties_1
*@Description:第一种方式:根据文件名使用Spring中的工具类进行解析
*filePath是相对路劲,文件需在classpath目录下
*比如:config.properties在包com.test.config下,
*路径就是com/test/config/config.properties
*
*@paramfilePath
*@returnvoid
*@throws
*/
publicstaticvoidgetProperties_1(StringfilePath){
Propertiesprop=null;
try{
//通过Spring中的PropertiesLoaderUtils工具类进行获取
prop=PropertiesLoaderUtils.loadAllProperties(filePath);
printAllProperty(prop);
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*根据key读取value
*
*@Title:getProperties_2
*@Description:第二种方式:使用缓冲输入流读取配置文件,然后将其加载,再按需操作
*绝对路径或相对路径,如果是相对路径,则从当前项目下的目录开始计算,
*如:当前项目路径/config/config.properties,
*相对路径就是config/config.properties
*
*@paramfilePath
*@paramkeyWord
*@return
*@returnString
*@throws
*/
publicstaticStringgetProperties_2(StringfilePath,StringkeyWord){
Propertiesprop=newProperties();
Stringvalue=null;
try{
//通过输入缓冲流进行读取配置文件
InputStreamInputStream=newBufferedInputStream(newFileInputStream(newFile(filePath)));
//加载输入流
prop.load(InputStream);
//根据关键字获取value值
value=prop.getProperty(keyWord);
}catch(Exceptione){
e.printStackTrace();
}
returnvalue;
}
/**
*读取配置文件所有信息
*
*@Title:getProperties_2
*@Description:第二种方式:使用缓冲输入流读取配置文件,然后将其加载,再按需操作
*绝对路径或相对路径,如果是相对路径,则从当前项目下的目录开始计算,
*如:当前项目路径/config/config.properties,
*相对路径就是config/config.properties
*
*@paramfilePath
*@returnvoid
*@throws
*/
publicstaticvoidgetProperties_2(StringfilePath){
Propertiesprop=newProperties();
try{
//通过输入缓冲流进行读取配置文件
InputStreamInputStream=newBufferedInputStream(newFileInputStream(newFile(filePath)));
//加载输入流
prop.load(InputStream);
printAllProperty(prop);
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*根据key读取value
*
*@Title:getProperties_3
*@Description:第三种方式:
*相对路径,properties文件需在classpath目录下,
*比如:config.properties在包com.test.config下,
*路径就是/com/test/config/config.properties
*@paramfilePath
*@paramkeyWord
*@return
*@returnString
*@throws
*/
publicstaticStringgetProperties_3(StringfilePath,StringkeyWord){
Propertiesprop=newProperties();
Stringvalue=null;
try{
InputStreaminputStream=TestProperties.class.getResourceAsStream(filePath);
prop.load(inputStream);
value=prop.getProperty(keyWord);
}catch(IOExceptione){
e.printStackTrace();
}
returnvalue;
}
/**
*读取配置文件所有信息
*
*@Title:getProperties_3
*@Description:第三种方式:
*相对路径,properties文件需在classpath目录下,
*比如:config.properties在包com.test.config下,
*路径就是/com/test/config/config.properties
*@paramfilePath
*@return
*@throws
*/
publicstaticvoidgetProperties_3(StringfilePath){
Propertiesprop=newProperties();
try{
InputStreaminputStream=TestProperties.class.getResourceAsStream(filePath);
prop.load(inputStream);
printAllProperty(prop);
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
//注意路径问题
Stringproperties_1=getProperties_1("com/test/config/config.properties","wechat_appid");
System.out.println("wechat_appid="+properties_1);
getProperties_1("com/test/config/config.properties");
System.out.println("*********************************************");
//注意路径问题
Stringproperties_2=getProperties_2("configure/configure.properties","jdbc.url");
System.out.println("jdbc.url="+properties_2);
getProperties_2("configure/configure.properties");
System.out.println("*********************************************");
//注意路径问题
Stringproperties_3=getProperties_3("/com/test/config/config.properties","wechat_appid");
System.out.println("wechat_appid="+properties_3);
getProperties_3("/com/test/config/config.properties");
}
}
到此这篇关于Java读取.properties配置文件的几种方式的文章就介绍到这了,更多相关Java读取properties配置内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。