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

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

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

    無線&移動互聯網技術研發

    換位思考·····
    posts - 19, comments - 53, trackbacks - 0, articles - 283
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    java properties 文件的加載

    Posted on 2009-06-07 01:02 Gavin.lee 閱讀(1303) 評論(0)  編輯  收藏 所屬分類: java SE & EE

    我們在項目中肯定會碰到很多配置文件情況,在這種情況下,加載屬性文件就是不可避免的了。
    我這里提供兩個方案:
    【一】直接在src目錄下或在其子包下:

    package com.Gavin.tools.des;

    import java.util.ResourceBundle;
    /**
     * @descripte load resource
     * 
    @author Gavin.lee
     * @date 2009-5-19上午09:49:32
     * 
    @version 1.0
     *
     
    */

    public class ResourceLoader {
        
    private ResourceBundle resBundle;
        
        
    public ResourceLoader(String resourceName) {
            resBundle 
    = ResourceBundle.getBundle(resourceName);
        }

        
        
    public String getString(String key) {
            
    return resBundle.getString(key);
        }

        
        
    public static void main(String args[]) {
            System.out.println(
    new ResourceLoader("config").getString("username"));        //config in src
            System.out.println(new ResourceLoader("doingjava.config").getString("username"));    //config in doingjava
        }

    }


    使用 Properties類中的load方法


    package com.yixun.wap;

    /** *//**
     * 
    @author Gavin.lee
     * @date 09-4-30 17:20pm
     * 
    @version 1.0
     
    */

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;

    public class LoadProperties {
        
    public void getSource(String source) {
            InputStream inputStream 
    = this.getClass().getClassLoader().getResourceAsStream(source);
            Properties prop 
    = new Properties();
            
    try {
                prop.load(inputStream);
            }
     catch (IOException e) {
                e.printStackTrace();
            }
            
            prop.setProperty(
    "truename""李顯武");
            System.out.println(
    "username:" + prop.getProperty("username"+ ",password:" + prop.getProperty("password"));
            System.out.println(
    "truename:" + prop.getProperty("truename"));        
            prop.clear();    
    //只想加載用一次的話 可以清空
            
    //        System.out.println("truename:" + prop.getProperty("truename"));
    //        System.out.println("username:" + prop.getProperty("username") + ",password:" + prop.getProperty("password"));
            
        }


        
    public static void main(String[] args) {

            
    new LoadProperties()..getSource("config.properties").getProperty("username");
        }


    }



    【二】可以指定properties 到自己想要的目錄下:


    package com.Gavin.tools.util;

    import java.io.File;
    import java.io.FileInputStream;
    import java.util.Properties;

    /** *//**
     * 
     * @descripte 任意加載properties文件
     * 
    @author Gavin.lee
     * @date 2009-6-7 0:55:18
     * 
    @version 1.0
     
    */

    public class ReadConfig {
        
    static String path = WriteLog.class.getResource("/").getPath();
        
    static String projectPath = path.substring(0, path.length()-16);// 到 WebRoot 下
        private static final String PFILE = projectPath + "config.properties";
            
        
    /** *//**  
         * 對應于屬性文件的文件對象變量  
         
    */

        
    private File m_file = null;

        
    /** *//**  
         * 屬性文件的最后修改日期  
         
    */

        
    private long m_lastModifiedTime = 0;

        
    /** *//**  
         * 屬性文件所對應的屬性對象變量  
         
    */

        
    private Properties m_props = null;

        
        
    private static ReadConfig m_instance = new ReadConfig();

        
    /** *//**  
         * 私有的構造器,用以保證外界無法直接實例化  
         
    */

        
    private ReadConfig() {
            m_file 
    = new File(PFILE);
            m_lastModifiedTime 
    = m_file.lastModified();
            
    if (m_lastModifiedTime == 0{
                System.err.println(PFILE 
    + " file does not exist!");
            }

            m_props 
    = new Properties();
            
    try {
                m_props.load(
    new FileInputStream(PFILE));
            }
     catch (Exception e) {
                e.printStackTrace();
            }

        }


        
    /** *//**  
         * 靜態工廠方法  
         * 
    @return 返還ReadConfig 類的單一實例  
         
    */

        
    synchronized public static ReadConfig getInstance() {
            
    return m_instance;
        }


        
    /** *//**  
         * 讀取一特定的屬性項  
         *  
         * 
    @param name 屬性項的項名
         * 
    @param defaultVal 屬性項的默認值  
         * 
    @return 屬性項的值(如此項存在), 默認值(如此項不存在)  
         
    */

        
    public String getConfigItem(String name, String defaultVal) {
            
    long newTime = m_file.lastModified();
            
    // 檢查屬性文件是否被其他程序
            
    // 如果是,重新讀取此文件

            
    if (newTime == 0{
                
    // 屬性文件不存在   
                if (m_lastModifiedTime == 0{
                    System.err.println(PFILE 
    + " file does not exist!");
                }
     else {
                    System.err.println(PFILE 
    + " file was deleted!!");
                }

                
    return defaultVal;
            }
     else if (newTime > m_lastModifiedTime) {
                
    // Get rid of the old properties   
                m_props.clear();
                
    try {
                    m_props.load(
    new FileInputStream(PFILE));
                }
     catch (Exception e) {
                    e.printStackTrace();
                }

            }

            m_lastModifiedTime 
    = newTime;
            String val 
    = m_props.getProperty(name);
            
    if (val == null{
                
    return defaultVal;
            }
     else {
                
    return val;
            }

        }


        
    /** *//**  
         * 讀取一特定的屬性項  
         *  
         * 
    @param name 屬性項的項名  
         * 
    @return 屬性項的值(如此項存在), 空(如此項不存在)  
         
    */

        
    public String getConfigItem(String name) {
            
    return getConfigItem(name, "");
        }

        
        
    public static void main (String args[]) {
            System.out.println(ReadConfig.getInstance().getConfigItem(
    "username"));
        }

    }


    properties :
    #url=wap.500wan.com
    username = doingjava
    password = lixianwu
    birthday =1986-06-03

    像這樣的properties文件,用上面的三個類都是任意可讀的。都在項目中試驗過,均可使用。 O(∩_∩)O~
                  ,%%%%%%%%,
               ,%%/\%%%%/\%%
              ,%%%\c "" J/%%%
     %.       %%%%/ o  o \%%%
     `%%.     %%%%    _  |%%%
      `%%     `%%%%(__Y__)%%'
      //       ;%%%%`\-/%%%'
     ((       /  `%%%%%%%'
      \\    .'          |
       \\  /       \  | |
        \\/         ) | |
         \         /_ | |__
         (___________)))))))【這個獅子很形象哈,呵呵】

     

    主站蜘蛛池模板: 美女被艹免费视频| 日韩免费视频播播| 一区二区三区福利视频免费观看| 全黄A免费一级毛片| 美女露隐私全部免费直播| 在线视频亚洲一区| 国产亚洲精品第一综合| 亚洲AV无码专区在线观看成人 | 色妞WWW精品免费视频| 日本视频一区在线观看免费| 亚洲黄色免费网站| 91嫩草免费国产永久入口| 免费观看激色视频网站bd | 亚洲AⅤ男人的天堂在线观看| 亚洲欧美一区二区三区日产| 亚洲人成色777777精品| 色噜噜的亚洲男人的天堂| 牛牛在线精品免费视频观看| 一级毛片不卡免费看老司机| 91av免费在线视频| 美女视频黄的免费视频网页| 最近免费中文字幕大全免费| 国产曰批免费视频播放免费s| 好先生在线观看免费播放| 在线观看免费国产视频| 亚洲另类少妇17p| 国产成人亚洲综合无码精品 | 毛片a级毛片免费观看免下载| 永久黄网站色视频免费直播| 亚洲不卡AV影片在线播放| 亚洲无线码一区二区三区| 亚洲不卡av不卡一区二区| 亚洲成aⅴ人在线观看| 亚洲乱码日产精品一二三| 深夜久久AAAAA级毛片免费看| 中文字幕在线视频免费观看 | 亚洲精品无码少妇30P| 九九全国免费视频| 免费无遮挡无码永久视频| 妞干网在线免费观看| 亚洲国产精品一区二区第四页|