Java 获取到配置文件信息

Java程序将数据库或者服务器IP写入到代码中,难免缺少灵活性。

如果写入到配置文件,部署到不通服务器上,只需要修改配置文

件即可。

Java怎么读取配置文件

 

	/**
	 * 获取到配置文件信息
	 * @param key
	 * @return
	 */
	public  String getConfigValue(String key){
		String path = this.getClass().getResource("/").getPath(); 
		InputStream ins =null;
		try {
			ins = new BufferedInputStream(new FileInputStream(System.getProperty("user.dir")+"/config/config.properties"));
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		}
		String value="";
		Properties properties = new Properties();
			try {
				properties.load(ins);
				value= properties.getProperty(key);
				ins.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		
			return value;
	}


配置文件格式:

pwd=root
username=root
driverName=com.mysql.jdbc.Driver
urlString=jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=UTF-8
htmlUrl=http://10.225.1.136/
downPath=E:\\勘探院采集文件源库\\维普\\
dataDownPath=E:\\\\勘探院采集文件源库\\\\维普\\\\
inputPage=28340


 

 

posted on 2013-12-13 16:43  love so much  阅读(385)  评论(0编辑  收藏  举报

导航