您的位置:首页 > 运维架构

通过Propertise读取配置文件

2010-05-18 20:23 309 查看
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Configuration {

private String address;
private String port;
private String username;
private String password;
public final static String CONFIG_FILE = "conf.properties";
public Configuration() {
// TODO Auto-generated constructor stub
}

public Configuration(String address, String port, String username,
String password) {
//super();
this.address = address;
this.port = port;
this.username = username;
this.password = password;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPort() {
return port;
}

public void setPort(String port) {
this.port = port;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public static Configuration getInstance() {
File file = new File (Configuration.CONFIG_FILE);
Properties prop = new Properties();
try {
prop.load(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String a = prop.getProperty("address");
String p = prop.getProperty("port");
String u = prop.getProperty("username");
String pwd = prop.getProperty("password");
return new Configuration(a, p, u, pwd);

}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration conf = Configuration.getInstance();
System.out.println(conf.getAddress());
System.out.println(conf.getPort());
System.out.println(conf.getUsername());
System.out.println(conf.getPassword());
}
}


相对应的Propertise文件内容

address=127.0.0.1
port=1521
username=test
password=test
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: