Java 操作Properties配置文件详解
1简介:
JDK提供的java.util.Properties类继承自Hashtable类并且实现了Map接口,是使用一种键值对的形式来保存属性集,其中键和值都是字符串类型。
java.util.Properties类提供了getProperty()和setProperty()方法来操作属性文件,同时使用load()方法和store()方法加载和保存Properties配置文件。
java.util.ResourceBundle类也提供了读取Properties配置文件的方法,ResourceBundle是一个抽象类。
2.Properties中的主要方法
1)load(InputStreaminStream):该方法可以从.properties属性文件对应的文件数入流中,加载属性列表到Properties类对象中。load有两个方法的重载:load(InputStreaminStream)、load(Readerreader),可根据不同的方式来加载属性文件。
InputStreaminStream=TestProperties.class.getClassLoader().getResourceAsStream("demo.properties");
//通过当前类加载器的getResourceAsStream方法获取
//TestProperties当前类名;TestProperties.class.取得当前对象所属的Class对象;getClassLoader():取得该Class对象的类装载器
InputStreamin=ClassLoader.getSystemResourceAsStream("filePath");
InputStreaminStream=newFileInputStream(newFile("filePath"));//从文件获取
InputStreamin=context.getResourceAsStream("filePath");//在servlet中,可以通过context来获取InputStream
InputStreaminStream=newURL("path").openStream();//通过URL来获取
读取方法如下:
Propertiespro=newProperties();//实例化一个Properties对象
InputStreaminStream=newFileInputStream("demo.properties");//获取属性文件的文件输入流
pro.load(nStream);
inStream.close();
2)store(OutputStreamout,Stringcomments):这个方法将Properties类对象的属性列表写入.properties配置文件。如下:
FileOutputStreamoutStream=newFileOutputStream("demo.properties");
pro.store(outStream,"Comment");
outStream.close();
3ResourceBundle中的主要方法
通过ResourceBundle.getBundle()静态方法来获取,此方法获取properties属性文件不需要加.properties后缀名。也可以从InputStream中获取ResourceBundle对象。
ResourceBundleresource=ResourceBundle.getBundle("com/xiang/demo");//emo为属性文件名,放在包com.xiang下,如果是放在src下,直接用test即可
ResourceBundleresource1=newPropertyResourceBundle(inStream);
Stringvalue=resource.getString("name");
在使用中遇到的问题可能是配置文件的路径,当配置文件不在当前类所在的包下,则需要使用包名限定;若属性文件在src根目录下,则直接使用demo.properties或demo即可。
4Properties操作实例
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;
importjava.util.Properties;
/**
*Java中Preperties配置文件工具类
*@authorshu
*
*/
publicclassPropsUtil{
privateStringpath="";
privatePropertiesproperties;
/**
*默认构造函数
*/
publicPropsUtil(){}
/**
*构造函数
*@parampath传入Properties地址值
*/
publicPropsUtil(Stringpath){
this.path=path;
}
/**
*加载properties文件
*@return返回读取到的properties对象
*/
publicPropertiesloadProps(){
InputStreaminStream=ClassLoader.getSystemResourceAsStream(path);
try{
if(inStream==null)
thrownewFileNotFoundException(path+"fileisnotfound");
properties=newProperties();
properties.load(inStream);
inStream.close();
}catch(IOExceptione){
e.printStackTrace();
}
returnproperties;
}
/**
*将配置写入到文件
*/
publicvoidwriteFile(){
//获取文件输出流
try{
FileOutputStreamoutputStream=newFileOutputStream(newFile(ClassLoader.getSystemResource(path).toURI()));
properties.store(outputStream,null);
outputStream.close();
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*通过关键字获取值
*@paramkey
*@return返回对应的字符串,如果无,返回null
*/
publicStringgetValueByKey(Stringkey){
if(properties==null)
properties=loadProps();
Stringval=properties.getProperty(key.trim());
returnval;
}
/**
*通过关键字获取值
*@paramkey需要获取的关键字
*@paramdefaultValue若找不到对应的关键字时返回的值
*@return返回找到的字符串
*/
publicStringgetValueByKey(Stringkey,StringdefaultValue){
if(properties==null)
properties=loadProps();
returnproperties.getProperty(key,defaultValue);
}
/**
*获取Properties所有的值
*@return返回Properties的键值对
*/
publicMapgetAllProperties(){
if(properties==null)
properties=loadProps();
Mapmap=newHashMap();
//获取所有的键值
Iteratorit=properties.stringPropertyNames().iterator();
while(it.hasNext()){
Stringkey=it.next();
map.put(key,properties.getProperty(key));
}
/*Enumerationenumeration=properties.propertyNames();
while(enumeration.hasMoreElements()){
Stringkey=(String)enumeration.nextElement();
Stringvalue=getValueByKey(key);
map.put(key,value);
}*/
returnmap;
}
/**
*往Properties写入新的键值且保存
*@paramkey对应的键
*@paramvalue对应的值
*/
publicvoidaddProperties(Stringkey,Stringvalue){
if(properties==null)
properties=loadProps();
properties.setProperty(key,value);
try{
writeFile();
}catch(Exceptione){
thrownewRuntimeException("writefail");
}
}
/**
*更新配置文件
*@paramkey对应的键
*@paramvalue对应的值
*/
publicvoidupdate(Stringkey,Stringvalue){
if(properties==null)
properties=loadProps();
if(properties.containsKey(key))
properties.replace(key,value);
try{
writeFile();
}catch(Exceptione){
thrownewRuntimeException("writefail");
}
}
/**
*刪除某一鍵值对
*@paramkey
*/
publicvoiddeleteByKey(Stringkey){
if(properties==null)
properties=loadProps();
if(!properties.containsKey(key))
thrownewRuntimeException("notsuchkey");
properties.remove(key);
try{
writeFile();
}catch(Exceptione){
thrownewRuntimeException("writefail");
}
}
/**
*设置path值
*@parampath
*/
publicvoidsetPath(Stringpath){
this.path=path;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。