org.eclipse.paho.client.mqttv3源碼分析,2類遍歷方式

每天一段源碼分析:

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();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章