posts - 70,comments - 408,trackbacks - 0

          發(fā)個(gè)例子大家自己看哈.
          package control;

          import java.io.BufferedInputStream;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.util.Enumeration;
          import java.util.Properties;

          public class TestMain {
          ?
          ?//根據(jù)key讀取value
          ?public static String readValue(String filePath,String key) {
          ??Properties props = new Properties();
          ??????? try {
          ??????? ?InputStream in = new BufferedInputStream (new FileInputStream(filePath));
          ??????? ?props.load(in);
          ??????? ?String value = props.getProperty (key);
          ??????????? System.out.println(key+value);
          ??????????? return value;
          ??????? } catch (Exception e) {
          ??????? ?e.printStackTrace();
          ??????? ?return null;
          ??????? }
          ?}
          ?
          ?//讀取properties的全部信息
          ??? public static void readProperties(String filePath) {
          ??? ?Properties props = new Properties();
          ??????? try {
          ??????? ?InputStream in = new BufferedInputStream (new FileInputStream(filePath));
          ??????? ?props.load(in);
          ??????????? Enumeration en = props.propertyNames();
          ??????????? ?while (en.hasMoreElements()) {
          ??????????? ??String key = (String) en.nextElement();
          ??????????????????? String Property = props.getProperty (key);
          ??????????????????? System.out.println(key+Property);
          ??????????????? }
          ??????? } catch (Exception e) {
          ??????? ?e.printStackTrace();
          ??????? }
          ??? }

          ??? //寫入properties信息
          ??? public static void writeProperties(String filePath,String parameterName,String parameterValue) {
          ??? ?Properties prop = new Properties();
          ??? ?try {
          ??? ??InputStream fis = new FileInputStream(filePath);
          ??????????? //從輸入流中讀取屬性列表(鍵和元素對(duì))
          ??????????? prop.load(fis);
          ??????????? //調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
          ??????????? //強(qiáng)制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。
          ??????????? OutputStream fos = new FileOutputStream(filePath);
          ??????????? prop.setProperty(parameterName, parameterValue);
          ??????????? //以適合使用 load 方法加載到 Properties 表中的格式,
          ??????????? //將此 Properties 表中的屬性列表(鍵和元素對(duì))寫入輸出流
          ??????????? prop.store(fos, "Update '" + parameterName + "' value");
          ??????? } catch (IOException e) {
          ??????? ?System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
          ??????? }
          ??? }

          ??? public static void main(String[] args) {
          ??? ?readValue("info.properties","url");
          ??????? writeProperties("info.properties","age","21");
          ??????? readProperties("info.properties" );
          ??????? System.out.println("OK");
          ??? }
          }

          posted on 2006-08-21 15:35 我心依舊 閱讀(62491) 評(píng)論(15)  編輯  收藏

          FeedBack:
          # re: 用JAVA輕松操作properties文件
          2008-05-09 17:02 | anlyhz@163.com
          有點(diǎn)收獲,嘿嘿。。。  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2008-05-31 11:07 | SMF
          不錯(cuò),這是我需要的,謝謝  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2008-09-02 14:46 | ghost
          # re: 用JAVA輕松操作properties文件
          2008-09-16 21:46 | 云飛揚(yáng)
          如果是在web項(xiàng)目里面,該如何配置和獲取properties文件呢?  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2008-09-18 06:51 | edwardpro
          都不finally的,運(yùn)行幾次就把io掛得死死的了.out少了flush,可能會(huì)有寫不進(jìn)去的問(wèn)題的.  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2008-11-19 15:23 | succ800
          對(duì)于初學(xué)的我來(lái)說(shuō),一個(gè)都看不懂,就知道幾個(gè)單詞
            回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2009-01-09 13:52 | 18w
          @succ800
          你不錯(cuò)了還看得懂單詞.。  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2009-03-19 14:53 | 雨帝夜淚
          哈哈,謝謝,剛好需要這方面的內(nèi)容  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2010-05-27 20:19 | bavol
          # re: 用JAVA輕松操作properties文件
          2010-09-16 10:19 | mick
          中文亂碼  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2011-02-25 13:48 | ChardRapid
          IO流都不關(guān)?這樣的代碼好好斟酌一下  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2011-07-05 00:02 | Ghost
          # re: 用JAVA輕松操作properties文件
          2011-08-25 14:43 |
          @ChardRapid
          你真2,人家只是給你個(gè)例子。  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2012-05-25 15:01 | pengguohui
          @云飛揚(yáng)
          以下估計(jì)是你想要的答案。
          /**
          * @author PENGGUOHUI
          * @explain以下方法是通過(guò)單例模式以及應(yīng)用反射機(jī)制取得屬性文件值。
          * @date 2012-5-25
          */
          private static Properties properties = new Properties();
          static{
          try {
          PoiWordTest.class.getClassLoader().getResourceAsStream("application.properties");//通過(guò)反射機(jī)制取得WEB工程ClassPath下屬性文件
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          public static String readURL(String key){
          return (String)properties.get(key);
          }  回復(fù)  更多評(píng)論
            
          # re: 用JAVA輕松操作properties文件
          2012-05-25 15:12 | pengguohui
          @pengguohui
          忘記給load了。
          重新添上代碼:
          /**
          * @author PENGGUOHUI
          * @explain以下方法是通過(guò)單例模式以及應(yīng)用反射機(jī)制取得屬性文件值。
          * @date 2012-5-25
          */
          private static Properties properties = new Properties();
          static{
          try {
          properties.load(PoiWordTest.class.getClassLoader().getResourceAsStream("application.properties"));//通過(guò)反射機(jī)制取得WEB工程ClassPath下屬性文件
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          public static String readURL(String key){
          return (String)properties.get(key);
          }  回復(fù)  更多評(píng)論
            

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 娱乐| 观塘区| 涿鹿县| 孝昌县| 广丰县| 洛阳市| 香港| 鄯善县| 沂源县| 孟连| 公安县| 普安县| 长子县| 霍邱县| 吉首市| 昂仁县| 延川县| 秦皇岛市| 云霄县| 达孜县| 霍州市| 十堰市| 微山县| 诸暨市| 桐城市| 阿图什市| 抚宁县| 荣昌县| 淄博市| 鄂尔多斯市| 和田市| 崇阳县| 桦甸市| 吴堡县| 嵊州市| 洛阳市| 黄大仙区| 陆丰市| 古浪县| 闻喜县| 新化县|