Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java从存在多次的config.properties文件中获取特定属性_Java_Iterator_Enumeration_Properties File - Fatal编程技术网

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

Java从存在多次的config.properties文件中获取特定属性,java,iterator,enumeration,properties-file,Java,Iterator,Enumeration,Properties File,我有一个包含名称的config.properties文件 说它看起来是这样的: #Client1 properties 1Client=Client1 1someproperty=someproperty #Client2 properties 2Client=Client1 2someproperty=someproperty 我试图实现的只是在DefaultListModel中获取客户机名称,以便将它们添加到列表中。我尝试了以下方法: public static DefaultListMo

我有一个包含名称的
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;
}
我一直在思考(objectobj:fis),但这是不可能的。另一个选项是使用所描述的迭代器。
虽然我不知道如何获得某个属性。

试试这样的方法。只是使用正则表达式来标识客户机属性。
@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;      
}
希望这有帮助

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;      
}
@测试
public void testClient()引发IOException{
DefaultListModel vm_list=新的DefaultListModel();
Properties props=新属性();
FileInputStream fis=null;
fis=新文件输入流(“config.properties”);
道具载荷(fis);
for(条目:props.entrySet()){
System.out.println(“输入键:+Entry.getKey()+”值:+Entry.getValue());
String key=(String)entry.getKey();
if(key.endsWith(“客户”)){
vm_list.addElement(key);//可能需要添加entry.getValue()
}
}
//返回vm_列表;
}
试试这个:-

使用域类进行管理

公共类客户端{
私有字符串客户端;
私有财产;
//默认构造函数
//参数化构造函数
//获取集方法
}
公共静态列表SetVM(){
List vm_List=new ArrayList();
//客户端代码示例
整数clientCodes[]={1,2};
Properties prop=新属性();
InputStream输入=null;
试一试{
输入=新文件输入流(“config.properties”);
//加载属性文件
道具载荷(输入);
//获取属性值
for(整数代码:clientCodes){
添加(新客户端(prop.getProperty(代码+“客户端”)、prop.getProperty(代码+“someproperty”));
}
返回vm_列表;
}捕获(IOEX异常){
例如printStackTrace();
}最后{
试一试{
input.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}

那么,您想获取其键以“Client”结尾的所有属性,对吗?迭代props的entrySet,测试条目的键是否以Client结尾,如果以Client结尾,则将值添加到模型中。当然,您必须在从inputstream加载属性之前创建inputstream。是的,正确。这有助于:)这是有效的。非常感谢。我很感激你花了这么多时间来写这段代码。这是我想要的。我知道获取值的键比只查找字符串更好。这使我对所采取的步骤更加清楚。如果我这样做,我将额外上一节课。在这种情况下,我看不到目的,因为我已经有了属性文件。