利用java.util.Properties读取属性文件
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,
如:properties
代码 ip=192.168.0.1
port=8080
则,我们可以用如下程序来获得服务器配置信息:
java 代码
[color=red][b]InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");[/b][/color]
Properties p = new Properties();
try{
p.load(inputStream);
}catch(IOException e1) {
e1.printStackTrace();
}
System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,
如:properties
代码 ip=192.168.0.1
port=8080
则,我们可以用如下程序来获得服务器配置信息:
java 代码
[color=red][b]InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");[/b][/color]
Properties p = new Properties();
try{
p.load(inputStream);
}catch(IOException e1) {
e1.printStackTrace();
}
System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));