posts - 70,comments - 408,trackbacks - 0

          發個例子大家自己看哈.
          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 {
          ?
          ?//根據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);
          ??????????? //從輸入流中讀取屬性列表(鍵和元素對)
          ??????????? prop.load(fis);
          ??????????? //調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
          ??????????? //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
          ??????????? OutputStream fos = new FileOutputStream(filePath);
          ??????????? prop.setProperty(parameterName, parameterValue);
          ??????????? //以適合使用 load 方法加載到 Properties 表中的格式,
          ??????????? //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
          ??????????? 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 我心依舊 閱讀(62514) 評論(15)  編輯  收藏

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

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


          網站導航:
           
          主站蜘蛛池模板: 咸宁市| 玉龙| 固原市| 北安市| 光泽县| 两当县| 莱西市| 延寿县| 青岛市| 金沙县| 尼勒克县| 奉节县| 南投市| 阿勒泰市| 莎车县| 抚宁县| 洪洞县| 岢岚县| 新竹县| 玉树县| 江孜县| 田林县| 伊川县| 佳木斯市| 临猗县| 泸水县| 胶南市| 蛟河市| 错那县| 封丘县| 通辽市| 濮阳市| 西吉县| 长兴县| 沾益县| 和林格尔县| 新田县| 武隆县| 山丹县| 和平区| 龙南县|