Java 获取属性文件apache commons中的条目数

Java 获取属性文件apache commons中的条目数,java,properties,apache-commons,Java,Properties,Apache Commons,我正在创建一个IP地址“to ping”列表,用户可以在其中添加到列表中,然后以site.name1=。。。site.name2= 目前我有一个固定数量的for循环,是否有方法获取属性文件中的条目数,以便我可以在for循环中设置它,而不是等待异常 PropertiesConfiguration config = configs.properties(new File("IPs.properties")); //initially check for how many v

我正在创建一个IP地址“to ping”列表,用户可以在其中添加到列表中,然后以site.name1=。。。site.name2=

目前我有一个固定数量的for循环,是否有方法获取属性文件中的条目数,以便我可以在for循环中设置它,而不是等待异常

 PropertiesConfiguration config = configs.properties(new File("IPs.properties"));
            //initially check for how many values there are - set to max increments for loop
            for (int i = 0; i < 3; i++) { //todo fix
                siteName = config.getString("site.name" + i);
                siteAddress = config.getString("site.address" + i);
                SiteList.add(i, siteName);
                IPList.add(i, siteAddress);
            }
PropertiesConfiguration config=configs.properties(新文件(“IPs.properties”);
//最初检查有多少个值-设置为循环的最大增量
对于(inti=0;i<3;i++){//todo-fix
siteName=config.getString(“site.name”+i);
siteAddress=config.getString(“site.address”+i);
站点列表。添加(i,站点名称);
添加(i,站点地址);
}

我已经浏览了文档和其他问题,但它们似乎不相关。

在我看来,基于文档,您应该能够使用PropertiesConfiguration#getLayout#getKeys以字符串的形式获取一组所有键

为了使用apache-commons-configuration-1.10,我不得不稍微修改一下代码

PropertiesConfiguration config=newpropertiesconfiguration(“ips.properties”);
PropertiesConfiguration布局=config.getLayout();
字符串siteName=null;
字符串siteAddress=null;
for(字符串键:layout.getKeys()){
字符串值=config.getString(键);
如果(值==null){
抛出新的IllegalStateException(String.format(“找不到键:%s的值”,键));
}
if(key.equals(“site.name”)){
siteName=值;
}else if(key.equals(“site.address”)){
siteAddress=值;
}否则{
抛出新的IllegalStateException(String.format(“不支持的键:%s”,键));
}
}
System.out.println(String.format(“名称=%s,地址=%s”,站点名称,站点地址));