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

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

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

    kooyee ‘s blog

    開源軟件, 眾人努力的結晶, 全人類的共同財富
    posts - 103, comments - 55, trackbacks - 0, articles - 66
       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    【Java】properties的使用

    Posted on 2007-10-05 23:15 kooyee 閱讀(765) 評論(1)  編輯  收藏 所屬分類: Java
    由于經常從文本讀寫屬性設置到代碼中,所以利用java的properties類帶取代對文件的操作。

    首先建立properties文本文件并輸入
    Host_name = database name...
    username = 用戶名
    password = 密碼

    讀取properties文件的代碼
    Properties props =  new Properties();
            props.load(
    new FileInputStream("./database.properties"));
            hostname 
    = props.getProperty("Host_name");
            username 
    = props.getProperty("User_name");
            password 
    = props.getProperty("Password");

    這樣在code外部(在properties文本文件中)修改屬性后, 通過properties導入到代碼中對應的值里。

    java properties 類
    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();

      }


    }

    評論

    # re: 【Java】properties的使用  回復  更多評論   

    2008-03-31 18:50 by 日月雨林
    謝謝啊,最近正在學習如何使用properties呢
    主站蜘蛛池模板: 亚洲永久永久永久永久永久精品| 美女巨胸喷奶水视频www免费| 久久精品国产亚洲沈樵| 成年人性生活免费视频| 午夜精品一区二区三区免费视频 | 伊人免费在线观看| 亚洲成AV人片在WWW| 亚洲色图综合网站| 亚洲人成网77777亚洲色| 免费a级毛片永久免费| 欧洲精品成人免费视频在线观看| 日韩精品免费在线视频| 国产高清视频免费在线观看| 国产精品亚洲综合一区在线观看| 亚洲性色精品一区二区在线| 亚洲人成7777影视在线观看| 亚洲邪恶天堂影院在线观看| 国产亚洲一区区二区在线| 亚洲第一黄片大全| 免费人成激情视频| 国产成人3p视频免费观看| 精品免费国产一区二区三区| 毛片免费观看的视频在线| 国产日本一线在线观看免费| 亚洲一级毛片免费看| 精品免费人成视频app| 亚洲精品国产免费| 91免费播放人人爽人人快乐| 3d成人免费动漫在线观看 | 亚洲一区影音先锋色资源| 国产亚洲精品资源在线26u| 亚洲热线99精品视频| 亚洲精品无码成人AAA片| 亚洲VA中文字幕无码毛片| 久久久久亚洲精品无码系列| 久久亚洲国产精品| 亚洲视频欧洲视频| 亚洲国产激情在线一区| 亚洲国产精品无码久久久秋霞1| 亚洲成AV人片高潮喷水| 一级毛片正片免费视频手机看|