java读取配置文件的方式

1.采用ClassLoader方式读取

private Properties load1() throws Exception{  
        properties = new Properties();  
        properties.load(Class.class.getResource("/ftp.properties").openStream());  
        return properties;  
}  

2.采用PropertiesLoaderUtils方式读取

private Properties load() {  
        properties = new Properties();  
        try {  
            properties = PropertiesLoaderUtils.loadAllProperties("ftp.properties");  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return properties;  
    }  

3.采用getResourceAsStream

private Properties load2() throws Exception{  
        properties = new Properties();  
        properties.load(  
                DailySupportUtils.class.getResourceAsStream("/ftp.properties"));  
        return properties;  
    }  

4.通过ResourceBundle资源包读取(配置文件在resource包中,配置文件不用加后缀名)

private List load4() throws Exception {  
        List list = new ArrayList<>();  
        ResourceBundle rb = ResourceBundle.getBundle("ftp");  
        Enumeration keys = rb.getKeys();  
        while (keys.hasMoreElements()) {  
            String key = keys.nextElement();  
            String value = rb.getString(key);  
            list.add(value);  
        }  
        return list;  
    }  

你可能感兴趣的:(java读取配置文件的方式)