背景:项目中有许多可选参数,这时如果采取硬编码的方式将非常不利于扩展,因此想到将参数写进配置文件,需要参数时再从配置文件中进行加载。
[color=red]调用方式:[/color]
[color=red]解析之读属性:[/color]
a、URL confFile = TestConfig.class.getClassLoader().getResource(configPath);//查找指定名称的资源,资源是可以通过类代码以与代码基无关的方式访问的某些数据(图像、声音、文本等)。
资源名称是以 '/' 分隔的标识资源的路径名称。
b、 in = confFile.openStream();//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。此方法是下面方法的缩写:
openConnection().getInputStream()
c、 p.load(in);//从输入流中读取属性列表(键和元素对)
d、String googleMapKey = getProperty(p, GOOGLE_MAP_KEY);//通过键搜索属性
[color=blue]疑问:为什么使用到了ClassLoader机制,直接读不行吗?[/color]
[color=red]解析之写属性:[/color]研究对象为writeProperty方法
a、 URLDecoder.decode(TestConfig.class.getClassLoader().getResource(configPath).getPath(), enc);//对路径进行解码
URLDecoder:HTML 格式解码的实用工具类。该类包含了将 String 从application/x-www-form-urlencoded MIME 格式解码的静态方法
b、 p.stringPropertyNames()返回此属性列表中的键集
public class TestConfig {
protected final static String enc = "utf-8";
private final static String LINE_SEPARATOR = System.getProperty("line.separator");
protected static Properties init(String configPath) {
Properties p = new Properties();
InputStream in = null;
try {
URL confFile = TestConfig.class.getClassLoader().getResource(configPath);
in = confFile.openStream();
p.load(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return p;
}
protected static String getProperty(Properties p, String key) {
return p.getProperty(key.trim());
}
protected static boolean writeProperty(Properties p, String configPath, String key, String value) throws IOException {
FileOutputStream out = new FileOutputStream(URLDecoder.decode(TestConfig.class.getClassLoader().getResource(configPath).getPath(), enc));
BufferedOutputStream bos = new BufferedOutputStream(out);
boolean isExistPropertyName = false;
try {
for (String tmpKey : p.stringPropertyNames()) {
String line;
if(key.equals(tmpKey)) {
isExistPropertyName = true;
p.setProperty(key, value);
line = tmpKey + "=" + value + LINE_SEPARATOR;
} else {
line = tmpKey + "=" + p.getProperty(tmpKey) + LINE_SEPARATOR;
}
byte[] linebytes = line.getBytes(enc);
bos.write(linebytes);
}
if(!isExistPropertyName) {
p.put(key, value);
String newLine = key + "=" + value + LINE_SEPARATOR;
bos.write(newLine.getBytes(enc));
}
return true;
} finally {
bos.flush();
bos.close();
}
}
}
[color=red]调用方式:[/color]
public class MyConfigUtil extends TesttConfig {
private static Properties p = new Properties();
private final static int DEFAULT_SECURITY_POLICY_PORT = 5907;
private final static String DEFAULT_GOOGLE_MAP_KEY = "ABQIAAAAWA_efwiQDHrv6MYDa3HE3hTwM0brOpm-All5BF6PoaKBxRWWERSSjUV6DjLhSErRl9rovZBNtOhfds";
private final static String DEFAULT_GOOGLE_MAP_SERVER_URL="http://ditu.google.cn/maps";
static {
p = init(CONFIG_PATH);
}
//google map key
public static String getGoogleMapKey() {
String googleMapKey = getProperty(p, GOOGLE_MAP_KEY);
if(googleMapKey == null || "".equals(googleMapKey)) {
return DEFAULT_GOOGLE_MAP_KEY;
}
return googleMapKey;
}
public static boolean saveGoogleMapKey(String value) throws IOException {
return writeProperty(p, CONFIG_PATH, GOOGLE_MAP_KEY, value);
}
//securityPort
public static int getSecurityPolicyPort() throws Exception {
File appFile = getFlexAppConfig();
int port = -1;
XMLBuilder builder = XMLBuilder.createBuilder(appFile);
NodeList policyList = builder.findNodeList(APPLICATION_SECURITY_POLICY_PORT);
if(policyList != null && policyList.getLength() > 0) {
Node node = policyList.item(0);
port = Integer.parseInt(node.getTextContent());
}
if(port == -1) {
return DEFAULT_SECURITY_POLICY_PORT;
}
return port;
}
private static File getFlexAppConfig() throws UnsupportedEncodingException {
String filePath = URLDecoder.decode(AbstractConfig.class.getClassLoader().getResource(APP_CONFIG).getPath(), enc);
return new File(filePath);
}
}
[color=red]解析之读属性:[/color]
a、URL confFile = TestConfig.class.getClassLoader().getResource(configPath);//查找指定名称的资源,资源是可以通过类代码以与代码基无关的方式访问的某些数据(图像、声音、文本等)。
资源名称是以 '/' 分隔的标识资源的路径名称。
b、 in = confFile.openStream();//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。此方法是下面方法的缩写:
openConnection().getInputStream()
c、 p.load(in);//从输入流中读取属性列表(键和元素对)
d、String googleMapKey = getProperty(p, GOOGLE_MAP_KEY);//通过键搜索属性
[color=blue]疑问:为什么使用到了ClassLoader机制,直接读不行吗?[/color]
[color=red]解析之写属性:[/color]研究对象为writeProperty方法
a、 URLDecoder.decode(TestConfig.class.getClassLoader().getResource(configPath).getPath(), enc);//对路径进行解码
URLDecoder:HTML 格式解码的实用工具类。该类包含了将 String 从application/x-www-form-urlencoded MIME 格式解码的静态方法
b、 p.stringPropertyNames()返回此属性列表中的键集