隨筆-16  評論-50  文章-2  trackbacks-0

          摘要:大多數應用程序都可以由用戶配置。配置文件是實現應用程序靈活性的一個有效手段。Hibernate、Spring等應用都需要配置文件。國際化的應用程序也是通過和Locale有關配置文件來實現的。

           

          下面的內容和Java應用程序配置有關。

          • properties
            • properties文件
            • 系統信息
          • preferences

           

          properties的使用

          加載

          Properties prop = new Properties();
          prop.load(new FileInputStream("filename"));
          String value = prop.getProperties("key");

           

          存儲

          Properties prop = new Properties();
          prop.setProperties("key", "value");
          ...
          prop.store(new FileOutputStream("filename.properties"), "comments");

           

          默認值

          prop.getProperties("key", "default");

          或者:

          Properties defaultProp = new Properties();
          defaultProp.setProperties("key", "value");
          ...

          Properties prop = new Properties(defaultProp);
          prop.load(new FileInputStream("filename"));
          prop.getProperties("key");

           

          漢字

          手寫的Properties文件,如果含有非ISO-8859-1的字符,就要用native2ascii.exe工具來編譯了。eclipse中也有工具可以實現該功能。native2ascii的使用很簡單。

           

          結構化的key

          font.name=Monospaced
          font.size=12

           

          枚舉key和values

          public Enumeration<?> propertyNames()

          size(), keys(), values

           

          XML

          <properties>
              <comment>comments</comment>
              <entry key="key">value</entry>
              ... ...
          </properties>

           

          Properties和Hashtabel的關系?

          Properties是Hashtable的子類。 這是一個壞的設計。Hashtable應當只是Properties的一個實例變量。

          class Properties {
              public String getProperty(String) {...}
              public String setProperty(String, String) {...}
              ...
              private Hashtable entry;
              protected Properties defaults;
          }

          在Properties的API中有下面的一段話,Sun通過文檔來彌補設計上的缺陷。

          因為 Properties 繼承于 Hashtable,所以可對 Properties 對象應用 put 和 putAll 方法。但強烈反對使用這兩個方法,因為它們允許調用方插入其鍵或值不是 Strings 的項。相反,應該使用 setProperty 方法。如果在“有危險”的 Properties 對象(即包含非 String 的鍵或值)上調用 store 或 save 方法,則該調用將失敗。
          posted on 2008-01-10 04:40 Jeff Lau 閱讀(1333) 評論(0)  編輯  收藏 所屬分類: Jeff On Java 2008

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 霞浦县| 大石桥市| 贵州省| 肇州县| 油尖旺区| 南京市| 大邑县| 右玉县| 湖口县| 尉氏县| 峨眉山市| 许昌市| 南城县| 梨树县| 屏东市| 灵川县| 新泰市| 盖州市| 平昌县| 沁源县| 易门县| 宝应县| 叶城县| 福安市| 麻栗坡县| 双鸭山市| 类乌齐县| 靖西县| 岐山县| 藁城市| 汤原县| 赣榆县| 上蔡县| 栾川县| 鹤壁市| 阜阳市| 佳木斯市| 阜宁县| 延边| 盐亭县| 开封市|