读取配置文件

 

读取配置文件

package com.seecen.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;

public class ReaderProperties {

    public static void main(String[] args) {
        Properties properties = new ReaderProperties().loadProperties("/jdbc.properties");
        Enumeration<?> enu = properties.propertyNames();  
        while (enu.hasMoreElements()) {  
            Object key = enu.nextElement();  
            System.out.println(key);  
        }  
        Enumeration<Object> enu2 = properties.elements();  
        while (enu2.hasMoreElements()) {  
            Object value = enu2.nextElement();  
            System.out.println(value);  
        }
        
        Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();  
        while (it.hasNext()) {  
            Entry<Object, Object> entry = it.next();  
            Object key = entry.getKey();  
            Object value = entry.getValue();  
            System.out.println("key   :" + key);  
            System.out.println("value :" + value);  
            System.out.println("---------------");  
        }  
    }

    
    public Properties loadProperties(String resources) {
        // 使用InputStream得到一个资源文件
        InputStream inputstream = this.getClass()
        .getResourceAsStream(resources);
        // new 一个Properties
        Properties properties = new Properties();
        try {
            // 加载配置文件
            properties.load(inputstream);
            return properties;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                inputstream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

 

posted @ 2015-02-07 17:16  江湖一笑  阅读(139)  评论(0编辑  收藏  举报