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

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


    網站導航:
     
    主站蜘蛛池模板: 中文字幕的电影免费网站| 77777亚洲午夜久久多喷| 特级一级毛片免费看| 国产真实伦在线视频免费观看| 亚洲欧美日韩综合久久久| 宅男666在线永久免费观看| 亚洲AV成人无码久久WWW| 亚洲AV日韩精品一区二区三区| 无遮挡a级毛片免费看| 免费国产美女爽到喷出水来视频| 美女被爆羞羞网站在免费观看| 亚洲国产精品成人久久蜜臀| a免费毛片在线播放| 国产亚洲综合成人91精品| 免费A级毛片在线播放| 亚洲国产美女视频| 毛色毛片免费观看| 理论片在线观看免费| 国产亚洲精品国产| 国产卡二卡三卡四卡免费网址| 亚洲色欲啪啪久久WWW综合网| 免费无码又爽又刺激高潮| 一级做a免费视频观看网站| 亚洲一区二区电影| 嫩草影院免费观看| 国产97视频人人做人人爱免费| 亚洲国产精品自在在线观看| 国产91免费在线观看| 国产亚洲视频在线播放大全| 日韩亚洲欧洲在线com91tv| 国国内清清草原免费视频99 | 亚洲国产成人无码AV在线| 又粗又黄又猛又爽大片免费| 永久免费AV无码网站国产| 亚洲黄色在线观看| 国产免费无遮挡精品视频| 暖暖免费日本在线中文| 亚洲国产精品无码久久98| 亚洲精品~无码抽插| 天天看片天天爽_免费播放| 中文字幕视频在线免费观看|