<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 我心依舊 閱讀(62500) 評論(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);
    }  回復  更多評論
      

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


    網站導航:
     
    主站蜘蛛池模板: 国产∨亚洲V天堂无码久久久 | 亚洲av成人一区二区三区在线播放| 一个人免费日韩不卡视频| 成人午夜亚洲精品无码网站| 永久免费无码网站在线观看个| 国产男女猛烈无遮挡免费视频| 色窝窝亚洲AV网在线观看| 国产精品酒店视频免费看| 国产成人 亚洲欧洲| 国产免费看插插插视频| 亚洲jizzjizz少妇| 亚洲免费在线观看视频| 国产精品爱啪在线线免费观看| 亚洲国产高清美女在线观看| 国产免费久久精品99re丫y| 亚洲人成人无码.www石榴| 国产精品视_精品国产免费| 亚州**色毛片免费观看| 国精无码欧精品亚洲一区 | 亚洲av无码一区二区三区不卡| 日本亚洲欧洲免费天堂午夜看片女人员| 亚洲性天天干天天摸| 99在线精品免费视频九九视| 亚洲砖码砖专无区2023| 五月婷婷亚洲综合| 老司机69精品成免费视频| 亚洲国产成人精品无码一区二区| 女性自慰aⅴ片高清免费| 亚洲大尺度无码专区尤物| 99久久人妻精品免费二区| 亚洲中文字幕久久精品无码VA| 又色又污又黄无遮挡的免费视| 亚洲一本综合久久| 青青青青青青久久久免费观看| 特级毛片在线大全免费播放| 亚洲一区二区三区首页| 国产成人aaa在线视频免费观看 | 亚洲午夜电影在线观看| 亚洲国产精品一区二区九九| 性xxxxx大片免费视频| 亚洲国产成人无码AV在线|