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<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;  
    }  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,966评论 25 709
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,993评论 6 342
  • 1. 我们没有电视。车水马龙、灯红酒绿、高楼幢幢,当外界种种浮夸躁了你的心,但愿还有一片场所让你静心。我们有一个小...
    泰安山房客栈阅读 160评论 0 0
  • 第二天下午放学,才到竹林边,弟弟就连蹦带跳的跑出来,笑着对姐姐说:“姐姐,姐姐,姐姐,大兔子死了!”姐姐恨了他一眼...
    疾静左剑阅读 463评论 2 3