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

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

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

    IT精英俱樂部

    方便自己查閱,僅此而已!

    統計

    留言簿(4)

    閱讀排行榜

    評論排行榜

    Java Properties 類讀取配置文件

     

    一. .properties 文件的形式

    # 以下為服務器、數據庫信息

    dbPort = localhost

    databaseName = mydb

    dbUserName = root

    dbPassword = root

    # 以下為數據庫表信息

    dbTable = mytable

    # 以下為服務器信息

    ip = 192.168.0.9

    在上面的文件中我們假設該文件名為: test.properties 文件。其中 # 開始的一行為注釋信息;在等號“ = ”左邊的我們稱之為 key ;等號“ = ”右邊的我們稱之為 value 。(其實就是我們常說的鍵 - 值對) key 應該是我們程序中的變量。而 value 是我們根據實際情況配置的。 

    二. JDK 中的 Properties Properties 類存在于胞 Java.util 中,該類繼承自 Hashtable ,它提供了幾個主要的方法:

    1 getProperty ( String key)    用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 key ,得到 key 所對應的 value

    2 load ( InputStream inStream) ,從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties 文件)進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key) 來搜索。

    3 setProperty ( String key, String value) ,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設置 - 值對。 

    4 store ( OutputStream out, String comments)    以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。

    5 clear () ,清除所有裝載的 - 值對。該方法在基類中提供。

    有了以上幾個方法我們就可以對 .properties 文件進行操作了!

    三.代碼實例

    package configuration;

    import java.io.FileInputStream;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.util.Properties;

    /** *//**

     * 讀取properties文件

     * @author Qutr

     *

     */

    public class Configuration

    ...{

        private Properties propertie;

        private FileInputStream inputFile;

        private FileOutputStream outputFile;

        /** *//**

         * 初始化Configuration

         */

        public Configuration()

        ...{

            propertie = new Properties();

        }

        /** *//**

         * 初始化Configuration

         * @param filePath 要讀取的配置文件的路徑+名稱

         */

        public Configuration(String filePath)

        ...{

            propertie = new Properties();

            try ...{

                inputFile = new FileInputStream(filePath);

                propertie.load(inputFile);

                inputFile.close();

            } catch (FileNotFoundException ex) ...{

                System.out.println("讀取屬性文件--->失敗!- 原因:文件路徑錯誤或者文件不存在");

                ex.printStackTrace();

            } catch (IOException ex) ...{

                System.out.println("裝載文件--->失敗!");

                ex.printStackTrace();

            }

        }//end ReadConfigInfo(...)

        /** *//**

         * 重載函數,得到key的值

         * @param key 取得其值的鍵

         * @return key的值

         */

        public String getValue(String key)

        ...{

            if(propertie.containsKey(key))...{

                String value = propertie.getProperty(key);//得到某一屬性的值

                return value;

            }

            else

                return "";

        }//end getValue(...)

        /** *//**

         * 重載函數,得到key的值

         * @param fileName properties文件的路徑+文件名

         * @param key 取得其值的鍵

         * @return key的值

         */

        public String getValue(String fileName, String key)

        ...{

            try ...{

                String value = "";

                inputFile = new FileInputStream(fileName);

                propertie.load(inputFile);

                inputFile.close();

                if(propertie.containsKey(key))...{

                    value = propertie.getProperty(key);

                    return value;

                }else

                    return value;

            } catch (FileNotFoundException e) ...{

                e.printStackTrace();

                return "";

            } catch (IOException e) ...{

                e.printStackTrace();

                return "";

            } catch (Exception ex) ...{

                ex.printStackTrace();

                return "";

           }

        }//end getValue(...)

        /** *//**

         * 清除properties文件中所有的key和其值

         */

        public void clear()

        ...{

            propertie.clear();

        }//end clear();

        /** *//**

         * 改變或添加一個key的值,當key存在于properties文件中時該key的值被value所代替,

         * key不存在時,該key的值是value

         * @param key 要存入的鍵

         * @param value 要存入的值

         */

        public void setValue(String key, String value)

        ...{

            propertie.setProperty(key, value);

        }//end setValue(...)

        /** *//**

         * 將更改后的文件數據存入指定的文件中,該文件可以事先不存在。

         * @param fileName 文件路徑+文件名稱

         * @param description 對該文件的描述

         */

        public void saveFile(String fileName, String description)

        ...{

            try ...{

                outputFile = new FileOutputStream(fileName);

                propertie.store(outputFile, description);

                outputFile.close();

            } catch (FileNotFoundException e) ...{

                e.printStackTrace();

            } catch (IOException ioe)...{

                ioe.printStackTrace();

            }

        }//end saveFile(...)

        public static void main(String[] args)

        ...{

            Configuration rc = new Configuration("."config"test.properties");//相對路徑

            String ip = rc.getValue("ipp");//以下讀取properties文件的值

            String host = rc.getValue("host");

            String tab = rc.getValue("tab");

            System.out.println("ip = " + ip + "ip-test leng = " + "ip-test".length());//以下輸出properties讀出的值

            System.out.println("ip's length = " + ip.length());

            System.out.println("host = " + host);

            System.out.println("tab = " + tab);

            Configuration cf = new Configuration();

            String ipp = cf.getValue("."config"test.properties", "ip");

            System.out.println("ipp = " + ipp);

    //        cf.clear();

            cf.setValue("min", "999");

            cf.setValue("max", "1000");

            cf.saveFile("."config"save.perperties", "test");

    //        Configuration saveCf = new Configuration();

    //        saveCf.setValue("min", "10");

    //        saveCf.setValue("max", "1000");

    //        saveCf.saveFile("."config"save.perperties");

        }//end main()

    }//end class ReadConfigInfo

    四.小結 通過上面的例子不難看出,在Java中操作配置文件是非常簡單的。在一個需要用到大量配置信息的模塊或系統里,我們有必要封裝一個專門的類來共使用。通過最后的main函數調用,相信大家可以看出該類的用法。不足指出希望大家多多指點。

    Java properties文件的操作 

    ----------------------------------------------------

           java中的properties文件是一種配置文件,主要用于表達配置信息,文件類型為*.properties,格式為文本文件,文件的內容是格式是"="的格式,在properties文件中,可以用"#"來作注釋,properties文件在Java編程中用到的地方很多,操作很方便。下面是一個操作java properties文件的例子,給出了操作方法和properties文件。從中可以看到如何讀取properties文件,并應用讀取出來的值,是學習操作properties文件的好例子。

    一、properties文件

    IcisReport.properties

    ------------------------------------------------------

    #################################

    #   工商報表應用IcisReport的配置文件       #

    #   作者:雷智民                                              #

    #   日期:20061121                           #

    #################################

    #

    #   說明:業務系統TopIcis和報表系統IcisReport是分離的

    #   可分開部署到不同的服務器上,也可以部署到同一個服務

    #   器上;IcisReprot作為獨立的web應用程序可以使用任何

    #   Servlet容器或者J2EE服務器部署并單獨運行,也可以

    #   通過業務系統的接口調用作為業務系統的一個庫來應用.

    #

    #   IcisReportip

    IcisReport.server.ip=192.168.3.143

    #   IcisReport的端口

    IcisReport.server.port=8080

    #   IcisReport的上下文路徑

    IcisReport.contextPath=/IcisReport

    ------------------------------------------------------ 

    二、操作properties文件的java方法 

    下面是一個操作properties文件的方法

        /** *//**

         * @return 獲取IcisReport報表應用的URL

         */

        private String getIcisReportURL() ...{

            String icisReportURL = ""; // IcisReport報表應用的URL

            String icisReportServerIP = ""; // IcisReport服務器的IP

            String icisReportServerPort = ""; // IcisReport服務器的服務端口

            String icisReportContextPath = ""; // IcisReport應用的ContextPath

            Properties prop = new Properties();

            InputStream in;

            try ...{

                in = getClass().getResourceAsStream("/IcisReport.properties");

                prop.load(in);

                Set keyValue = prop.keySet();

                for (Iterator it = keyValue.iterator(); it.hasNext();) ...{

                    String key = (String) it.next();

                    if (key.equals("IcisReport.server.ip")) ...{

                        icisReportServerIP = (String) prop.get(key);

                    } else if (key.equals("IcisReport.server.port")) ...{

                        icisReportServerPort = (String) prop.get(key);

                    } else if (key.equals("IcisReport.contextPath")) ...{

                        icisReportContextPath = (String) prop.get(key);

                    }

                }

            } catch (Exception e) ...{

                log.error("IO讀取出錯,找不到IcisReport.properties!");

            }

            if (icisReportServerIP.trim().equals("")) ...{

                log

                        .error("請檢查配置文件IcisReport.properties中的IcisReport.server.ip項的值是否正確!");

            }

            if (icisReportServerPort.trim().equals("")) ...{

                log

                        .error("請檢查配置文件IcisReport.properties中的IcisReport.server.port項的值是否正確!");

            }

            if (icisReportServerPort.trim().equals("")) ...{

                log

                        .error("請檢查配置文件IcisReport.properties中的IcisReport.server.port項的值是否正確!");

            }

            icisReportURL = "http://" + icisReportServerIP.trim() + ":"

                    + icisReportServerPort.trim() + icisReportContextPath.trim();

           log.info("獲取的icisReportURL=" + icisReportURL);

            return icisReportURL;

        }

    總結:javaproperties文件需要放到classpath下面,這樣程序才能讀取到,有關classpath實際上就是java類或者庫的存放路徑,在java工程中,properties放到class文件一塊。在web應用中,最簡單的方法是放到web應用的WEB-INF"classes目錄下即可,也可以放在其他文件夾下面,這時候需要在設置classpath環境變量的時候,將這個文件夾路徑加到classpath變量中,這樣也也可以讀取到。在此,你需要對classpath有個深刻理解,classpath絕非系統中刻意設定的那個系統環境變量,WEB-INF"classes其實也是,java工程的class文件目錄也是。

    該文章轉載自網絡大本營:http://www.xrss.cn/Dev/JAVA/200761514149.Html

    posted on 2008-10-20 04:58 suplayer 閱讀(750) 評論(0)  編輯  收藏 所屬分類: Java SE

    主站蜘蛛池模板: 亚洲中文字幕伊人久久无码| 国产乱子伦片免费观看中字| 久久精品国产精品亚洲蜜月| 老妇激情毛片免费| 波多野结衣中文一区二区免费| 亚洲成在人线在线播放无码| 天天干在线免费视频| 亚洲精品GV天堂无码男同| 真实乱视频国产免费观看| 亚洲狠狠色丁香婷婷综合| 免费a级毛片视频| 国产福利在线观看永久免费| 国产亚洲情侣一区二区无码AV| 中文字幕在线成人免费看| 国产亚洲综合久久系列| 在线看片免费人成视久网| 亚洲乱码一二三四区麻豆| 精品国产免费一区二区| 黄网站色视频免费看无下截 | 国产午夜亚洲不卡| 成年免费a级毛片免费看无码| 日本亚洲欧洲免费天堂午夜看片女人员 | 国产免费观看网站| 一级毛片免费不卡| 午夜影视日本亚洲欧洲精品一区| 最近高清中文字幕免费| 亚洲欧美成人一区二区三区| 亚洲视频在线一区二区| 91精品全国免费观看含羞草| 成人区精品一区二区不卡亚洲| 四虎影视永久免费观看| 99re6在线精品免费观看| 亚洲日本在线观看网址| 国产精品国产免费无码专区不卡| 一级免费黄色毛片| 亚洲国产片在线观看| 亚洲国产精品第一区二区三区| 久久国产精品免费网站| 亚洲精品国产suv一区88| 亚洲动漫精品无码av天堂| 最近最新MV在线观看免费高清|