java读取配置文件的方式

来源:互联网 发布:连点器的编程 编辑:程序博客网 时间:2024/05/16 04:58

1.采用ClassLoader方式读取

private Properties load1() throws Exception{<span style="white-space:pre"></span>properties = new Properties();<span style="white-space:pre"></span>properties.load(<span style="white-space:pre"></span>DailySupportUtils.class.getResource("/ftp.properties").openStream());<span style="white-space:pre"></span>return properties;<span style="white-space:pre"></span>}

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;}




0 0