通过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
相关文章推荐
- 通过servletcontext读取WEB-INF下的配置文件
- 在XPO中怎么通过配置文件来读取数据库链接
- C#通过读取配置文件里的内容连接数据库
- Java中通过dom4j读取配置文件实现抽象工厂+反射
- Java中通过dom4j读取配置文件实现抽象工厂+反射
- android应用面试宝典(下);用自定义组件控制循环组件使用bug还添加标志显示答案标志位;通过包名读取配置文件AndroidManifest.xml
- Java中通过dom4j读取配置文件实现抽象工厂+反射
- AntisHsu studying 通过读取.properties配置文件中的键值来实现动态添加注解内容例如@Controller @RequestMapping(value = "${adminPa
- 【java基础:JDBC】通过读取配置文件进行数据库连接并读取数据的Demostration
- 通过python读取ini配置文件
- 通过IO流读取配置文件
- 通过读取配置文件数据库连接
- Unity Application Block--通过配置文件读取映射
- SpringMVC通过@Value读取配置文件
- 通过读取web.xml文件中的配置参数连接数据库(原)
- Spring MVC 通过 @PropertySource和@Value 来读取配置文件
- 通过Spring读取properties配置文件的信息 Spring 读取properties
- 通过静态代码块读取配置文件配置静态属性
- java 通过 Properties 读取数据库配置 .properties 文件的使用。
- 通过spring工厂读取property配置文件