每天一段源码分析:
import java.util.Properties;
private static final String[] propertyKeys = new String[]{"com.ibm.ssl.protocol", "com.ibm.ssl.contextProvider", "com.ibm.ssl.keyStore", "com.ibm.ssl.keyStorePassword", "com.ibm.ssl.keyStoreType", "com.ibm.ssl.keyStoreProvider", "com.ibm.ssl.keyManager", "com.ibm.ssl.trustStore", "com.ibm.ssl.trustStorePassword", "com.ibm.ssl.trustStoreType", "com.ibm.ssl.trustStoreProvider", "com.ibm.ssl.trustManager", "com.ibm.ssl.enabledCipherSuites", "com.ibm.ssl.clientAuthentication"};
private void checkPropertyKeys(Properties properties) throws IllegalArgumentException {
Set keys = properties.keySet();
Iterator i = keys.iterator();
while(i.hasNext()) {
String k = (String)i.next();
if (!this.keyValid(k)) {
throw new IllegalArgumentException(k + " is not a valid IBM SSL property key.");
}
}
}
//Set 的应用;
//Iterator 遍历器的应用;
// 私有函数 验证字符串数组中有没有传入的关键字
private boolean keyValid(String key) {
int i;
for(i = 0; i < propertyKeys.length && !propertyKeys[i].equals(key); ++i) {
}
return i < propertyKeys.length;
}
String [] 数组中保存确定的属性,私有函数负责遍历检查传入的属性是不是数组中的成员。如果数组中没有,就抛出异常---不合法的参数异常。
2、 java.util.Properties学习
/**
* The {@code Properties} class represents a persistent set of
* properties. The {@code Properties} can be saved to a stream
* or loaded from a stream. Each key and its corresponding value in
* the property list is a string.
java.util.Properties是对properties这类配置文件的映射。支持key-value类型和xml类型两种。
测试,使用:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
* @program: spring-cloud-kubeedge
* @description: 配置文件解析的代码
* @author: Miller.FAN
* @create: 2019-12-17 15:40
**/
public class ReadProperties {
public static void main(String[] args) {
Properties prop = new Properties();
try {
FileInputStream fis = new FileInputStream("D:\\spring-cloud-kubeedge\\spring-cloud-kebeedge-protocol\\src\\main\\resources\\application.properties");
try {
prop.load(fis);
prop.list(System.out);
System.out.println("\nThe password property: " +
prop.getProperty("password"));
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}