<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

    發(fā)個例子大家自己看哈.
    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);
    ??????????? //從輸入流中讀取屬性列表(鍵和元素對)
    ??????????? 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 我心依舊 閱讀(62497) 評論(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);
    }  回復  更多評論
      

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


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: caoporn成人免费公开| yellow视频免费在线观看| 亚洲a∨无码精品色午夜| 国产亚洲精品欧洲在线观看| selaoban在线视频免费精品| 在线观看免费无码专区| 精品国产免费人成电影在线观看 | 久久精品国产亚洲av麻| 久久亚洲私人国产精品| 国产午夜亚洲精品国产| 免费夜色污私人影院网站| 国产情侣久久久久aⅴ免费 | 国产亚洲Av综合人人澡精品| 国产福利电影一区二区三区,免费久久久久久久精| 中文字幕av无码不卡免费| 免费看男女下面日出水来| 四虎成人精品在永久免费| 国产亚洲精品a在线无码| 亚洲熟妇无码久久精品| 在线亚洲v日韩v| 日韩免费无码视频一区二区三区| 久久av免费天堂小草播放| 亚洲美女视频免费| xvideos亚洲永久网址| 亚洲视频在线观看| 亚洲一久久久久久久久| www永久免费视频| 一色屋成人免费精品网站| 亚洲无线一二三四区手机| 亚洲精品中文字幕麻豆| 一区二区三区AV高清免费波多| 老司机午夜在线视频免费观| 久久亚洲免费视频| 免费在线看片网站| 亚洲美女激情视频| 国产精品免费一区二区三区| a毛片基地免费全部视频| 久久久久亚洲av成人无码电影 | 亚洲成a人片在线观| 亚洲视频在线免费| 女人张开腿给人桶免费视频 |