如何一次性将整个properties文件里面的配置内容读取出来,现在我想到的就是直接读取文件的方式,但是这种方式读取的效率不行吧,求大神解答。。。(分数不多,谅解)
------解决思路----------------------
package com.mkyong.properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class App {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
------解决思路----------------------
Properties本身就是个Hashtable,可以像HashMap那样遍历出来。
------解决思路----------------------
想这样,就不要弄成properties文件啊,直接xml,读取整个xml,转成map或json,想怎么玩怎么玩
------解决思路----------------------
楼上已经给出代码了,如果楼主觉得想要更强大功能可以采用apache-commons-configuration;支持getInt等类型的获取
进入http://commons.apache.org/下载