美文网首页
java读取配置文件的方式

java读取配置文件的方式

作者: 发光的鱼 | 来源:发表于2017-03-20 12:26 被阅读0次

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

相关文章

网友评论

      本文标题:java读取配置文件的方式

      本文链接:https://www.haomeiwen.com/subject/vcibnttx.html