posts - 33,  comments - 17,  trackbacks - 0
           1/*
           2操作屬性文件,可以為我們的程序帶來(lái)更方便的移植性,下面是一個(gè)示例,可以讀、寫(xiě)、更改屬性
           3讀采用了兩種方式,一種是采用Properties類,另外一種是采用資源綁定類ResourceBundle類,
           4下面是源程序,里面有詳細(xì)的注釋:
           5*/

           6import java.io.FileInputStream;
           7import java.io.FileOutputStream;
           8import java.io.InputStream;
           9import java.util.Properties;
          10import java.util.ResourceBundle;
          11/**
          12 *對(duì)屬性文件(xx.properties)的操作
          13 *注:屬性文件一定要放在當(dāng)前工程的根目錄下,也就是放在與src目錄在同一個(gè)目錄下(我的JDevelop
          14 *是這樣的)
          15 */

          16public class OperatePropertiesFile {
          17    public OperatePropertiesFile() {
          18    }

          19    /**
          20     *采用Properties類取得屬性文件對(duì)應(yīng)值
          21     *@parampropertiesFileNameproperties文件名,如a.properties
          22     *@parampropertyName屬性名
          23     *@return根據(jù)屬性名得到的屬性值,如沒(méi)有返回""
          24     */

          25    public static String getValueByPropertyName(String propertiesFileName,String propertyName) {
          26        String s="";
          27        Properties p=new Properties();//加載屬性文件讀取類
          28        FileInputStream in;
          29        try {
          30            //propertiesFileName如test.properties
          31            in = new FileInputStream(propertiesFileName);//以流的形式讀入屬性文件
          32            p.load(in);//屬性文件將該流加入的可被讀取的屬性中
          33            in.close();//讀完了關(guān)閉
          34            s=p.getProperty(propertyName);//取得對(duì)應(yīng)的屬性值
          35        }
           catch (Exception e) {
          36            e.printStackTrace();
          37        }

          38        return s;
          39    }

          40    /**
          41     *采用ResourceBundel類取得屬性文件對(duì)應(yīng)值,這個(gè)只能夠讀取,不可以更改及寫(xiě)新的屬性
          42     *@parampropertiesFileNameWithoutPostfixproperties文件名,不帶后綴
          43     *@parampropertyName屬性名
          44     *@return根據(jù)屬性名得到的屬性值,如沒(méi)有返回""
          45     */

          46    public static String getValueByPropertyName_(String propertiesFileNameWithoutPostfix,String propertyName) {
          47        String s="";
          48        //如屬性文件是test.properties,那此時(shí)propertiesFileNameWithoutPostfix的值就是test
          49        ResourceBundle bundel = ResourceBundle.getBundle(propertiesFileNameWithoutPostfix);
          50        s=bundel.getString(propertyName);
          51        return s;
          52    }

          53    /**
          54     *更改屬性文件的值,如果對(duì)應(yīng)的屬性不存在,則自動(dòng)增加該屬性
          55     *@parampropertiesFileNameproperties文件名,如a.properties
          56     *@parampropertyName屬性名
          57     *@parampropertyValue將屬性名更改成該屬性值
          58     *@return是否操作成功
          59     */

          60    public static boolean changeValueByPropertyName(String propertiesFileName,String propertyName,String propertyValue) {
          61        boolean writeOK=true;
          62        Properties p=new Properties();
          63        InputStream in;
          64        try {
          65            
          66            in = new FileInputStream(propertiesFileName);
          67            p.load(in);//
          68            in.close();
          69            p.setProperty(propertyName,propertyValue);//設(shè)置屬性值,如不屬性不存在新建
          70            //p.setProperty("testProperty","testPropertyValue");
          71            FileOutputStream out=new FileOutputStream(propertiesFileName);//輸出流
          72            p.store(out,"");//設(shè)置屬性頭,如不想設(shè)置,請(qǐng)把后面一個(gè)用""替換掉
          73            out.flush();//清空緩存,寫(xiě)入磁盤(pán)
          74            out.close();//關(guān)閉輸出流
          75        }
           catch (Exception e) {
          76            e.printStackTrace();
          77        }

          78        return writeOK;
          79    }

          80}

          81
          posted on 2008-07-23 17:54 scea2009 閱讀(461) 評(píng)論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          PL/SQL存儲(chǔ)過(guò)程與函數(shù)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 涡阳县| 左权县| 齐齐哈尔市| 静宁县| 平凉市| 泾源县| 盐亭县| 马边| 卢龙县| 永丰县| 即墨市| 夏津县| 阿合奇县| 嘉义市| 乐清市| 涟水县| 广丰县| 夏津县| 托克托县| 大庆市| 新竹市| 铜川市| 泰宁县| 赣榆县| 泸定县| 丹东市| 祁连县| 商洛市| 淮安市| 花垣县| 黑龙江省| 贡山| 通化县| 建水县| 龙胜| 马鞍山市| 兴业县| 伊吾县| 定西市| 宝鸡市| 剑河县|