本篇文章主要介绍了java 操作properties配置文件详解,详细的介绍了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(inputstream instream):该方法可以从.properties属性文件对应的文件数入流中,加载属性列表到properties类对象中。load有两个方法的重载:load(inputstream instream)、load(reader reader),可根据不同的方式来加载属性文件。
inputstream instream = testproperties.class.getclassloader().getresourceasstream("demo.properties");
//通过当前类加载器的getresourceasstream方法获取
//testproperties当前类名;testproperties.class.取得当前对象所属的class对象; getclassloader():取得该class对象的类装载器
inputstream in = classloader.getsystemresourceasstream("filepath");
inputstream instream = new fileinputstream(new file("filepath")); //从文件获取
inputstream in = context.getresourceasstream("filepath"); //在servlet中,可以通过context来获取inputstream
inputstream instream = new url("path").openstream(); //通过url来获取
读取方法如下:
properties pro = new properties(); //实例化一个properties对象
inputstream instream = new fileinputstream("demo.properties"); //获取属性文件的文件输入流
pro.load(nstream);
instream.close();
2)store(outputstream out,string comments):这个方法将properties类对象的属性列表写入.properties配置文件。如下:
fileoutputstream outstream = new fileoutputstream("demo.properties");
pro.store(outstream,"comment");
outstream.close();
3 resourcebundle中的主要方法
通过resourcebundle.getbundle()静态方法来获取,此方法获取properties属性文件不需要加.properties后缀名。也可以从inputstream中获取resourcebundle对象。
resourcebundle resource = resourcebundle.getbundle("com/xiang/demo");//emo为属性文件名,放在包com.xiang下,如果是放在src下,直接用test即可
resourcebundle resource1 = new propertyresourcebundle(instream);
string value = resource.getstring("name");
在使用中遇到的问题可能是配置文件的路径,当配置文件不在当前类所在的包下,则需要使用包名限定;若属性文件在src根目录下,则直接使用demo.properties或demo即可。
4 properties操作实例
import java.io.file;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.util.hashmap;
import java.util.iterator;
import java.util.map;
import java.util.properties;
/**
* java中preperties配置文件工具类
* @author shu
*
*/
public class propsutil {
private string path = "";
private properties properties ;
/**
* 默认构造函数
*/
public propsutil() {}
/**
* 构造函数
* @param path 传入properties地址值
*/
public propsutil(string path) {
this.path = path;
}
/**
* 加载properties文件
* @return 返回读取到的properties对象
*/
public properties loadprops(){
inputstream instream = classloader.getsystemresourceasstream(path);
try {
if(instream==null)
throw new filenotfoundexception(path + " file is not found");
properties = new properties();
properties.load(instream);
instream.close();
} catch (ioexception e) {
e.printstacktrace();
}
return properties;
}
/**
* 将配置写入到文件
*/
public void writefile(){
// 获取文件输出流
try {
fileoutputstream outputstream = new fileoutputstream( new file(classloader.getsystemresource(path).touri()));
properties.store(outputstream, null);
outputstream.close();
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
/**
* 通过关键字获取值
* @param key
* @return 返回对应的字符串,如果无,返回null
*/
public string getvaluebykey(string key) {
if(properties==null)
properties = loadprops();
string val = properties.getproperty(key.trim());
return val;
}
/**
* 通过关键字获取值
* @param key 需要获取的关键字
* @param defaultvalue 若找不到对应的关键字时返回的值
* @return 返回找到的字符串
*/
public string getvaluebykey(string key,string defaultvalue){
if(properties==null)
properties = loadprops();
return properties.getproperty(key, defaultvalue);
}
/**
* 获取properties所有的值
* @return 返回properties的键值对
*/
public map<string, string> getallproperties() {
if(properties==null)
properties = loadprops();
map<string, string> map = new hashmap<string, string>();
// 获取所有的键值
iterator<string> it=properties.stringpropertynames().iterator();
while(it.hasnext()){
string key=it.next();
map.put(key, properties.getproperty(key));
}
/*enumeration enumeration = properties.propertynames();
while (enumeration.hasmoreelements()) {
string key = (string) enumeration.nextelement();
string value = getvaluebykey(key);
map.put(key, value);
}*/
return map;
}
/**
* 往properties写入新的键值且保存
* @param key 对应的键
* @param value 对应的值
*/
public void addproperties(string key, string value) {
if(properties==null)
properties = loadprops();
properties.setproperty(key, value);
try {
writefile();
} catch (exception e) {
throw new runtimeexception("write fail");
}
}
/**
* 更新配置文件
* @param key 对应的键
* @param value 对应的值
*/
public void update(string key,string value){
if(properties==null)
properties = loadprops();
if(properties.containskey(key))
properties.replace(key, value);
try {
writefile();
} catch (exception e) {
throw new runtimeexception("write fail");
}
}
/**
* 刪除某一鍵值对
* @param key
*/
public void deletebykey(string key){
if(properties==null)
properties = loadprops();
if(!properties.containskey(key))
throw new runtimeexception("not such key");
properties.remove(key);
try {
writefile();
} catch (exception e) {
throw new runtimeexception("write fail");
}
}
/**
* 设置path值
* @param path
*/
public void setpath(string path){
this.path = path;
}
}
【相关推荐】
1. 特别推荐:“php程序员工具箱”v0.1版本下载
2. java免费视频教程
3. java教程手册
以上就是操作properties的java代码详解的详细内容。