网站首页 > 网络编程教程 > Java教程 > java获取properties配置文件

java获取properties配置文件

  • 作者:互联网
  • 时间:2009-12-02 16:46:11

-
package test.bwl;   
  
import ja***io.FileNotFoundException;   
import ja***io.IOException;   
import ja***io.InputStream;   
import ja***util.Properties;   
  
public class Test {   
    private static Properties properties = new Properties();   
  
    public static void main(String[] args) {   
        try {   
            InputStream is = Te***class.getClassLoader().getResourceAsStream("ca***.properties");   
            pr***rties.load(is);   
            String size = pr***rties.getProperty("ca***.size");   
            writeLog("配置成功!" + size);   
        } catch (FileNotFoundException e) {   
            writeLog("配置文件不存在!" + e.***Message());   
        } catch (IOException e) {   
            writeLog("读取配置文件IO错误!" + e.***Message());   
        }   
    }   
  
    public static void writeLog(String strLog) {   
        Sy***m.out.println(strLog);   
    }   
}  
package test.bwl;

import ja***io.FileNotFoundException;
import ja***io.IOException;
import ja***io.InputStream;
import ja***util.Properties;

public class Test {
private static Properties properties = new Properties();

public static void main(String[] args) {
try {
InputStream is = Te***class.getClassLoader().getResourceAsStream("ca***.properties");
pr***rties.load(is);
String size = pr***rties.getProperty("ca***.size");
writeLog("配置成功!" + size);
} catch (FileNotFoundException e) {
writeLog("配置文件不存在!" + e.***Message());
} catch (IOException e) {
writeLog("读取配置文件IO错误!" + e.***Message());
}
}

public static void writeLog(String strLog) {
Sy***m.out.println(strLog);
}
}

 

package test.bwl;   
  
import ja***io.File;   
import ja***io.FileInputStream;   
import ja***io.FileNotFoundException;   
import ja***io.FilenameFilter;   
import ja***io.IOException;   
import ja***io.InputStream;   
import ja***util.Collections;   
import ja***util.HashMap;   
import ja***util.Map;   
import ja***util.Properties;   
import ja***util.regex.Pattern;   
  
/**  
* 配置信息管理器  
*   
* @author      bwl   
* @version     1.0  
*/  
public class ConfigManager {   
  
    /**  
     * 提供单例对象的静态内部类  
     */  
    private static class SingletonHolder {   
        public static ConfigManager instance = new ConfigManager();   
    }   
  
    /**  
     * 获取对象实例  
     * @return  
     */  
    public static ConfigManager getInstance() {   
        return Si***etonHolder.instance;   
    }   
  
    /**  
     * 存储问题列表的Map  
     */  
    private Map name2properties;   
  
    /**  
     * 构造方法,请使用getInstance()获取实例  
     */  
    private ConfigManager() {   
        name2properties = Co***ctions.synchronizedMap(new HashMap());   
        doInit();   
    }   
  
    /**  
     * 初始化方法   
     */  
    private void doInit() {   
        try {   
            File path = new File("./conf/");   
            if (!pa***exists()) {   
                Sy***m.out.println("ConfilgManager Init Error: There is no folder named 'conf' under src file.");   
                return;   
            }   
            File[] confFiles = pa***listFiles(new DirFilter(".*\.properties"));//\   
            for (int i = 0; i < co***iles.length; i++) {   
                File f = confFiles[i];   
                if (f.exists() && f.isFile()) {   
                    Properties properties = new Properties();   
                    InputStream is = new FileInputStream(f);   
                    pr***rties.load(is);   
                    na***properties.put(f.***Name(), properties);   
                }   
            }   
  
        } catch (FileNotFoundException e) {   
            e.***ntStackTrace();   
        } catch (IOException e) {   
            e.***ntStackTrace();   
        }   
    }   
  
    /**  
     * 获取配置项的值  
     * @param fileName  配置文件的名称  
     * @param key       关键码的值  
     * @return  配置项  
     */  
    public String getProperty(String fileName, String key) {   
        if (fileName == null || fi***ame.length() == 0) {   
            return null;   
        }   
        Properties prop = na***properties.get(fileName);   
        if (prop != null) {   
            return pr***getProperty(key);   
        }   
        return null;   
    }   
  
    /**  
     * 获取整形的配置项的值  
     * @param fileName  配置文件的名称  
     * @param keyName   关键码的值  
     * @return  如果正确则返回数字,否则返回-1  
     */  
    public int getIntProperty(String fileName, String key) {   
        String value = th***getProperty(fileName, key);   
        int result = -1;   
        if (value == null) {   
            return result;   
        }   
        try {   
            result = In***er.parseInt(value);   
            return result;   
        } catch (Exception e) {   
            //Do nothing   
        }   
        return result;   
    }   
  
    /**  
     * 过滤属性文件的内部类   
     */  
    class DirFilter implements FilenameFilter {   
  
        /**  
         * 记录文件名格式的正则对象  
         */  
        private Pattern pattern;   
  
        public DirFilter(String regex) {   
            pattern = Pa***rn.compile(regex);   
        }   
  
        public boolean accept(File dir, String name) {   
            return pa***rn.matcher(new File(name).getName()).matches();   
        }   
  
    }   
  
    public static void main(String[] args) {   
        ConfigManager config = Co***gManager.getInstance();   
        Sy***m.out.println(co***g.getIntProperty("ca***.properties", "ca***.size") + "");   
        Sy***m.out.println(co***g.getProperty("ja***roups.properties", "bus_name") + "");   
    }   
 

上一篇: java遍历对象

下一篇: SQL 通配符