[轉(zhuǎn)]java.util.Preferences 數(shù)據(jù)管理

          java.util.Preferences 這是一個抽象類,具體有什么用呢?
          主要用于保存用戶或系統(tǒng)相關(guān)的數(shù)據(jù)(  比如: 我們希望某個用戶設(shè)置自己的主題,下次再登錄進系統(tǒng)時,仍然是這個主題...  ),具體的作用功能什么的我不做多的介紹...
              由于這個類屬于抽象類,所以不能直接通過new來創(chuàng)建,不過它提供了幾個static method ,其實底層還是通過調(diào)用子類的實現(xiàn)來操作的..
              Preferences的數(shù)據(jù)是可以跨平臺的,在Windows下,設(shè)置的這些屬性會添加到'注冊表'中,Linux 或 Unix 我沒有研究過...
               
           
              不多說,直接貼出源代碼(代碼中有詳細的注釋):
          package com.lixing.util;

          import java.io.File;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.OutputStream;
          import java.util.prefs.BackingStoreException;
          import java.util.prefs.Preferences;

          /**
          * Preferences有兩種保存文件的方式:
          *    1. sysRoot 與系統(tǒng)有關(guān)的用戶配置文件,此文件只要是系統(tǒng)用戶都可以訪問
          *    2. userRoot 與用戶有關(guān)的用戶配置文件,只有該用戶才可以訪問
          *    但兩者的操作方法完全一樣
          *    
          * @author Administrator
          *
          */
          public class PreferencesTest {
            private String[] keys = { "userName", "userPwd", "userEmail" };
            private String[] values = { "Lixing", "123456", "hnlixing520@126.com" };

            /**
              * 設(shè)置Preferences的值
              * @param userRoot
              */
            private    void setPreference(Preferences userRoot) {
              for (int i = 0; i < keys.length; i++) {
                userRoot.put(keys[i], values[i]);
              }

            }
              
            /**
              * 獲得Preferences的指定值
              * @param userRoot
              */
            private void getPreference(Preferences userRoot){
              System.out.println( "userName: " + userRoot.get("userName", "佚名") );
              System.out.println( "userPwd: " + userRoot.get("userPwd", "0000") );
              System.out.println( "userEmail: " + userRoot.get("userEmail", "xxx@126.com") );
            }
              
            /**
              * 移除Preferences的首層包
              * @param userRoot
              */
            private void removeNode(Preferences userRoot){
              try {
                userRoot.removeNode();
              } catch (BackingStoreException e) {
                e.printStackTrace();
              }
              System.out.println( "removeNode success" );
            }

            /**
              * 以樹型結(jié)構(gòu)導出為XML
              * @param userRoot
              */
            private void exportSubTree(Preferences userRoot) {
              OutputStream os = null;
              File file =new File("preferences.xml");
              try {
                os = new FileOutputStream( file );
                userRoot.exportSubtree(os);
              } catch (FileNotFoundException e) {
                e.printStackTrace();
              } catch (IOException e) {
                e.printStackTrace();
              } catch (BackingStoreException e) {
                e.printStackTrace();
              } finally {
                try {
                  os.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
              System.out.println( "export tree success filename " + file.getName() );
            }
              
              

            public static void main(String[] args) {
              PreferencesTest test = new PreferencesTest();
              
              //以包的方式建立用戶配置,目的就是防止沖突,便于管理
              Preferences userRoot = Preferences.userNodeForPackage(PreferencesTest.class);
              test.setPreference(userRoot);
              test.exportSubTree(userRoot);
              test.getPreference(userRoot);
              test.removeNode(userRoot);
            }
          }
           

          本文出自 “李新博客” 博客,請務(wù)必保留此出處http://kinglixing.blog.51cto.com/3421535/968777

          posted on 2012-08-22 13:48 Dragon4s 閱讀(195) 評論(0)  編輯  收藏 所屬分類: Java


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


          網(wǎng)站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統(tǒng)計

          留言簿

          文章分類(6)

          文章檔案(6)

          最新隨筆

          搜索

          最新評論

          主站蜘蛛池模板: 黎城县| 襄樊市| 邢台市| 荆门市| 扎囊县| 额济纳旗| 东兴市| 宁城县| 正镶白旗| 布拖县| 分宜县| 安丘市| 清远市| 沙洋县| 西畴县| 昭苏县| 松潘县| 乐昌市| 金乡县| 沂水县| 鄂托克旗| 无极县| 香格里拉县| 日土县| 濮阳市| 沙河市| 怀集县| 游戏| 仁寿县| 甘南县| 资兴市| 延寿县| 天全县| 调兵山市| 芦山县| 花莲市| 买车| 邢台县| 辽阳县| 洞头县| 沈阳市|