小石頭
          Excellence in any department can be attained only by the labor of a lifetime; it is not to be purchased at a lesser price.
          posts - 91,comments - 22,trackbacks - 0

          本文給出了一個(gè)例子,說明了如何利用configuration對properties的屬性進(jìn)行添加刪除修改操作

          下載地址 :  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 這種發(fā)方式同上
          #colors.pie = #FF0000;
          #colors.pie = #00FF00;
          #colors.pie = #0000FF;

          application.name = Killer App
          application.version = 1.6.2

          application.title = ${application.name} ${application.version}

          ?

          實(shí)現(xiàn)類 :

          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>文件描述:? 它還提供了自動(dòng)存盤和自動(dòng)更新的功能:Automatic Reloading Automatic Saving</p>
          ?* <p>版權(quán)所有: 版權(quán)所有(C)2001-2004</p>
          ?* <p>公??? 司: </p>
          ?* <p>內(nèi)容摘要: 無</p>
          ?* <p>其他說明: 無</p>
          ?* <p>創(chuàng)建日期:2006-11-9</p>
          ?* <p>完成日期:2006-11-9</p>
          ?* <p>修改記錄1: // 修改歷史記錄,包括修改日期、修改者及修改內(nèi)容</p>
          ?* <pre>
          ?*??? 修改日期:
          ?*??? 版 本 號:
          ?*??? 修 改 人:
          ?*??? 修改內(nèi)容:
          ?* </pre>
          ?* <p>修改記錄2:…</p>
          ?* @version 1.0
          ?* @author?楊威?*/

          /**
          ?* @author yangwei0048700358
          ?*
          ?*/
          public class Properties {
          ??? public static void readProperties() {
          ??????? try {

          ??????????? //讀取常規(guī)數(shù)據(jù)
          ??????????? 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);

          ??????????? //在這個(gè)usergui.properties 文件中也可以引用其它的文件,具體語法如下:
          //??????????? # usergui.properties
          //
          //??????????? include = colors.properties
          //??????????? include = sizes.properties

          ??????????? //Lists and arrays 讀取數(shù)組屬性

          ??????????? //以數(shù)組的方式獲得
          //??????????? 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(){
          ??????? //出始化一個(gè)文件
          ??????? addProperties();
          ??????? File file = new File("addProperties.properties");
          //????? 寫入數(shù)據(jù)
          ??????? 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(){
          ??????? //出始化一個(gè)文件
          ??????? 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();
          ??????? }

          ??????? //寫入數(shù)據(jù)
          ??????? 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 小石頭 閱讀(1980) 評論(0)  編輯  收藏 所屬分類: Jakarta Commons
          主站蜘蛛池模板: 松江区| 黄冈市| 且末县| 莒南县| 诸城市| 武安市| 黎平县| 辽宁省| 江安县| 彭阳县| 双桥区| 白朗县| 宽甸| 虹口区| 威信县| 醴陵市| 祁阳县| 东兰县| 彰武县| 吉木萨尔县| 铁力市| 肃南| 汕头市| 柳河县| 内丘县| 马尔康县| 武川县| 南溪县| 连南| 邹城市| 黄龙县| 青冈县| 汝阳县| 白玉县| 棋牌| 塔河县| 灯塔市| 穆棱市| 昌图县| 农安县| 抚远县|