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

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

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

    Java學習

    java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

     

    java中Properties類的使用

    package com.adrop.util;

     

    import Java.io.*;

    import java.util.Properties;

    import javax.servlet.http.*;

    import javax.servlet.*;

    import javax.servlet.jsp.*;

     

    public class PropertiesUtil {

      private String fileName;

      private Properties p;

      private FileInputStream in;

    private FileOutputStream out;

      /**

       * 根據傳進的文件名載入文件

       * @param fileName String

       */

      public PropertiesUtil(String fileName) {

        this.fileName=fileName;

        File file = new File(fileName);

        try {

          in = new FileInputStream(file);

          p = new Properties();

          //載入文件

          p.load(in);

          in.close();

        }

        catch (FileNotFoundException e) {

          System.err.println("配置文件config.properties找不到!!");

          e.printStackTrace();

        }

        catch (Exception e) {

          System.err.println("讀取配置文件config.properties錯誤!!");

          e.printStackTrace();

        }

      }

     

      /**

       * 配置文件一律為config.propertities,并且統一放在web應用的根目錄下。

       * @return String

       */

      public static String getConfigFile(HttpServlet hs) {

     

        return getConfigFile(hs,"config.properties");

      }

      /**

       * 在servlet中使用,直接用this作為參數,HttpServlet類型

       * 根據配置文件名從當前web應用的根目錄下找出配置文件

       * @param hs HttpServlet

       * @param configFileName String配置文件名字

       * @return String

       */

      public static String getConfigFile(HttpServlet hs, String configFileName) {

        String configFile = "";

        ServletContext sc = hs.getServletContext();

        configFile = sc.getRealPath("/" + configFileName);

        if (configFile == null configFile.equals("")) {

          configFile = "/" + configFileName;

        }

        return configFile;

      }

      /**

       * jsp中用pageContext作參數

       * @param hs PageContext

       * @param configFileName String 配置文件名字

       * @return String

       */

      public static String getConfigFile(PageContext hs, String configFileName) {

        String configFile = "";

        ServletContext sc = hs.getServletContext();

        configFile = sc.getRealPath("/" + configFileName);

        if (configFile == null configFile.equals("")) {

          configFile = "/" + configFileName;

        }

        return configFile;

      }

     

      /**

       * 列出所有的配置文件內容

       */

      public void list() {

        p.list(System.out);

      }

     

      /**

       * 指定配置項名稱,返回配置值

       * @param itemName String

       * @return String

       */

      public String getValue(String itemName){

        return p.getProperty(itemName);

      }

     

      /**

       * 指定配置項名稱和默認值,返回配置值

       * @param itemName String

       * @param defaultValue String

       * @return String

       */

      public String getValue(String itemName,

                             String defaultValue){

        return p.getProperty(itemName,defaultValue);

      }

     

      /**

       * 設置配置項名稱及其值

       * @param itemName String

       * @param value String

       */

      public void setValue(String itemName,String value){

        p.setProperty(itemName,value);

        return;

      }

     

      /**

       * 保存配置文件,指定文件名和抬頭描述

       * @param fileName String

       * @param description String

       * @throws Exception

       */

      public void saveFile(String fileName,String description)throws Exception{

        try {

          File f=new File(fileName);

          out

              = new FileOutputStream(f);

          p.store(out, description);//保存文件

          out.close();

        }

        catch (IOException ex) {

          throw new Exception

              ("無法保存指定的配置文件:"+fileName);

        }

      }

     

      /**

       * 保存配置文件,指定文件名

       * @param fileName String

       * @throws Exception

       */

      public void saveFile(String fileName)

          throws Exception {

        saveFile(fileName,"");

      }

     

      /**

       * 保存配置文件,采用原文件名

       * @throws Exception

       */

      public void saveFile() throws Exception {

        if(fileName.length()==0)

          throw new Exception

              ("需指定保存的配置文件名");

        saveFile(fileName);

      }

      /**

       * 刪除一個屬性

       * @param value String

       */

      public void deleteValue(String value){

        p.remove(value);

      }

      /**

       * main method for test

       * @param args String[]

       */

      public static void main(String[] args) {

        String file = "f:\\p.properties";

        PropertiesUtil pu = new PropertiesUtil(file);

        pu.list();

      }


    private String getParam(String file) {
       Properties p = new Properties();
       try {
        InputStream in = getClass().getResourceAsStream(file);
        p.load(in);
        if (in != null)
         in.close();

       } catch (Exception e) {
        System.out.println("file not found");
       } finally {
        return p.getProperty("ip");
       }

    }





    }

    posted on 2008-11-26 17:03 找個美女做老婆 閱讀(318) 評論(0)  編輯  收藏


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


    網站導航:
     

    導航

    統計

    公告

    本blog已經搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲GV天堂无码男同在线观看| 亚洲特级aaaaaa毛片| 男女猛烈xx00免费视频试看| 亚洲成在人线aⅴ免费毛片| 亚洲人成网站在线观看播放青青| 亚洲第一网站免费视频| 亚洲国产人成在线观看| 91免费国产自产地址入| 亚洲一卡2卡3卡4卡国产网站 | 亚洲精品美女久久久久9999| 久久午夜免费鲁丝片| 亚洲视频在线免费观看| 韩国日本好看电影免费看| 亚洲人成未满十八禁网站| 日韩在线天堂免费观看| 在线精品自拍亚洲第一区| 亚洲第一网站男人都懂| jyzzjyzz国产免费观看| 精品国产综合成人亚洲区| 亚洲日韩精品无码专区网址| 国产成人精品高清免费| 日本在线观看免费高清| 国产亚洲综合成人91精品| 暖暖日本免费中文字幕| 亚洲gv白嫩小受在线观看| 国内精品免费视频精选在线观看| 亚洲美女色在线欧洲美女| 最新猫咪www免费人成| 免费VA在线观看无码| 亚洲乱码国产一区三区| 麻豆一区二区免费播放网站| 国产成人不卡亚洲精品91| 国产美女亚洲精品久久久综合| 亚洲免费观看在线视频| 国产成人人综合亚洲欧美丁香花| 国产综合精品久久亚洲| 国产91免费在线观看| 立即播放免费毛片一级| 亚洲另类激情综合偷自拍| 拨牐拨牐x8免费| 光棍天堂免费手机观看在线观看|