本文给出了一个例子,说明了如何利用configuration对properties的属性进行添加删除修改操作
下载地址 : http://jakarta.apache.org/commons/configuration
依赖jar :
commons-collections-3.2.jar
commons-configuration-1.3.jar
commons-lang-2.2.jar
commons-logging-1.1.jar
commons-logging-adapters-1.1.jar
commons-logging-api-1.1.jar
读取properties文件的例子:
配置文件:
addProperties.properties
greeting = Hello,how are you?
colors.background = #FFFFFFFF
colors.pie = #FF0000/#00FF00/#0000FF
colors.graph = #808080/#00FFCC/#6422FF
usergui.properties
# Properties definining the GUI
colors.background = #FFFFFF
colors.foreground = #000080
window.width = 500
window.height = 300
# chart colors
colors.pie = #FF0000, #00FF00, #0000FF
# chart colors 这种发方式同上
#colors.pie = #FF0000;
#colors.pie = #00FF00;
#colors.pie = #0000FF;
application.name = Killer App
application.version = 1.6.2
application.title = ${application.name} ${application.version}
实现类 :
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
/**
* <p>文件名称: Properties.java </p>
* <p>文件描述: 它还提供了自动存盘和自动更新的功能:Automatic Reloading Automatic Saving</p>
* <p>版权所有: 版权所有(C)2001-2004</p>
* <p>公 司: </p>
* <p>内容摘要: 无</p>
* <p>其他说明: 无</p>
* <p>创建日期:2006-11-9</p>
* <p>完成日期:2006-11-9</p>
* <p>修改记录1: // 修改历史记录,包括修改日期、修改者及修改内容</p>
* <pre>
* 修改日期:
* 版 本 号:
* 修 改 人:
* 修改内容:
* </pre>
* <p>修改记录2:…</p>
* @version 1.0
* @author 杨威 */
/**
* @author yangwei0048700358
*
*/
public class Properties {
public static void readProperties() {
try {
//读取常规数据
PropertiesConfiguration config = new PropertiesConfiguration(
"usergui.properties");
String backColor = config.getString("colors.background");
Dimension size = new Dimension(config.getInt("window.width"),
config.getInt("window.height"));
System.out.println("height is " + size.getHeight() + " width is " +
size.getWidth() + " backGround is" + backColor);
//在这个usergui.properties 文件中也可以引用其它的文件,具体语法如下:
// # usergui.properties
//
// include = colors.properties
// include = sizes.properties
//Lists and arrays 读取数组属性
//以数组的方式获得
// String[] colors = config.getStringArray("colors.pie");
//list方式
List colorList = config.getList("colors.pie");
Iterator iterator=colorList.iterator();
while(iterator.hasNext()){
System.out.println("the element is "+iterator.next());
}
//Variable Interpolation
String title = config.getString("application.title");
System.out.println("title is "+title);
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void setProperties(){
//出始化一个文件
addProperties();
File file = new File("addProperties.properties");
// 写入数据
PropertiesConfiguration config=null;
try
{
config = new PropertiesConfiguration(file.getAbsolutePath());
}
catch (ConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
config.setProperty("colors.background", "#FFFFFFFF");
try
{
config.save();
//另一种存盘的方式 config.save("usergui.backup.properties);
}
catch (ConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void addProperties(){
//出始化一个文件
File file = new File("addProperties.properties");
if(file.exists()){
file.delete();
}
try
{
file.createNewFile();
System.out.println("文件出始化成功");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//写入数据
PropertiesConfiguration config=null;
try
{
config = new PropertiesConfiguration(file.getAbsolutePath());
}
catch (ConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// Change the list delimiter character to a slash
config.setListDelimiter('/');
// Now add some properties
config.addProperty("greeting", "Hello, how are you?");
config.addProperty("colors.background", "#000000");
config.addProperty("colors.pie",
new String[] { "#FF0000", "#00FF00", "#0000FF" });
config.addProperty("colors.graph", "#808080/#00FFCC/#6422FF");
// Access data
String salut = config.getString("greeting");
System.out.println("salut : "+salut);
String[] colGraph = config.getStringArray("colors.graph");
System.out.println("colGraph : "+colGraph);
String firstPieColor = config.getString("colors.pie");
//另一种方式List colPie = config.getList("colors.pie");
System.out.println("firstPieColor : "+firstPieColor);
//存盘
try
{
config.save();
}
catch (ConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
readProperties();
setProperties();
// TODO Auto-generated method stub
}
}
posted on 2007-01-10 11:19
小石头 阅读(1973)
评论(0) 编辑 收藏 所属分类:
Jakarta Commons