为了程序容易修改,不用把接口的的信息放在程序里面,如果是那样,接口的端口改变就会造成很麻烦,所以把它写在配置文件properties中
1.新建一个配置文件property 如:test.properties
2.新建一个类 PropertiesUti.Class
package com.jh.hiv.constant;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class PropertiesUtil {
private static Properties _prop = new Properties();
/**
* 读取配置文件
* @param fileName
*/
public static void readProperties(String fileName){
try {
InputStream in = PropertiesUtil.class.getResourceAsStream("/"+fileName);
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
_prop.load(bf);
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 根据key读取对应的value
* @param key
* @return
*/
public static String getProperty(String key){
return _prop.getProperty(key);
}
}
3.在书写程序的内容类中加上
PropertiesUtil.readProperties("jdbc1.properties");
String driverName=PropertiesUtil.getProperty("name");
String dbURL=PropertiesUtil.getProperty("name");
String userName =PropertiesUtil.getProperty("name");
String userPwd=PropertiesUtil.getProperty("name");
这样就可以了,就容易修改配置文件了
转载于:https://blog.51cto.com/11657004/1891712