Java 从多次存在的 config.properties 文件中获取某个 属性

Java getting a certain property from the config.properties file that exists more than once

我有 config.properties 个包含名称的文件。

这样说:

#Client1 properties
1Client=Client1
1someproperty=someproperty
#Client2 properties
2Client=Client1
2someproperty=someproperty

我想要实现的只是在 DefaultListModel 中获取客户端名称,这样我就可以将它们添加到列表中。我尝试了以下方法:

public static DefaultListModel SetVM() {
    int clientCode = 1;
    DefaultListModel vm_list = new DefaultListModel();
    Properties props = new Properties();
    FileInputStream fis = null;
    try {
        props.load(fis);
        fis = new FileInputStream("config.properties");
        //if (statement to see if props contains clientCode) {
            String vmClient = props.getProperty(DatabaseCode + "CLIENT");
            vm_list.addElement(vmClient);
        //}
    } catch (Exception e) {
    }
    return vm_list;
}

我的想法是 for(Object obj : fis),但这是不可能的。另一种选择是使用 here 中描述的迭代器。 虽然我无法掌握如何获得某个 属性.

尝试这样的事情。刚刚使用正则表达式来识别客户端 属性。 希望这有帮助。

Properties properties = new Properties();

    try
    {
        properties.load(new FileInputStream("config.properties"));
        for(String key : properties.stringPropertyNames()){
            if(key.matches("(\d+)Client")){
                //add to arrayList
            }
        }
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

试试这个。使用 Properties entrySet 为您提供每个 属性.

的键和值
@Test
public void testClient () throws IOException {

    DefaultListModel<String> vm_list = new DefaultListModel<String>();
    Properties props = new Properties();
    FileInputStream fis = null;
    fis = new FileInputStream("config.properties");
    props.load(fis);
    for (Entry<Object, Object> entry : props.entrySet()) {
        System.out.println("Entry key:" + entry.getKey() + " value:" + entry.getValue());
        String key = (String)entry.getKey();
        if (key.endsWith("Client")) {
            vm_list.addElement(key); // maybe want to add entry.getValue() instead
        }
    }
    // return vm_list;      
}

试试这个:-

Manage with a domain class

public class Client {

private String client;
private String someproperty;

// default constructor
// parameterized constructor
// get set methods
}



public static List<Client> SetVM() {

        List<Client> vm_list = new ArrayList<Client>();

        // client codes example
        Integer clientCodes[] = { 1, 2 };

        Properties prop = new Properties();
        InputStream input = null;
        try {
            input = new FileInputStream("config.properties");
            // load a properties file
            prop.load(input);

            // get the property values
            for (Integer code : clientCodes) {
                vm_list.add(new Client(prop.getProperty(code + "Client"), prop.getProperty(code + "someproperty")));
            }
            return vm_list;
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }