读取Properties 文件

    /**
     * 读取配置文件
     */
    public static Properties loadParams(String file) throws IOException {

        Properties prop = new Properties();
        ResourceBundle bundle = ResourceBundle.getBundle(file);

        String key = null;
        for (Enumeration enuma = bundle.getKeys(); enuma
                .hasMoreElements(); ) {
            key = enuma.nextElement();
            prop.put(key, bundle.getObject(key));
        }
        return prop;
    }

    /**
     * 获取外部配置文件
     */
    public static Properties loadOutParams(String file) throws IOException {

        Properties prop = new Properties();
        String proFilePath = System.getProperty("user.dir") + File.separator + file;
        InputStream in = new BufferedInputStream(new FileInputStream(proFilePath));
        ResourceBundle bundle = new PropertyResourceBundle(in);

        String key = null;
        for (Enumeration enuma = bundle.getKeys(); enuma
                .hasMoreElements(); ) {
            key = enuma.nextElement();
            prop.put(key, bundle.getObject(key));
        }
        return prop;
    }



=======================================================================

Properties pro = Utilties.loadOutParams("/config/config.properties");
username = pro.getProperty("username");
password = pro.getProperty("pwd");

读取Properties 文件_第1张图片

你可能感兴趣的:(Java后台)