java 属性中具有多个值的单个键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11557298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:33:40  来源:igfitidea点击:

Single key with multiple values in property

java

提问by Beginner

I having a property file containing below data :

我有一个包含以下数据的属性文件:

acqurierSystemAlias=CTC0,CTC1,CTC2,CTC3,CTC4,FEXCO,AMEX,DINERS

now in the main program :

现在在主程序中:

String acqurierSA = "CTC1";
String[] acqurierSystemAlias = properties.getProperty("acqurierSystemAlias").split(",");

for(String xyz: acqurierSystemAlias){
    if(xyz.equalsIgnoreCase(acqurierSA)) {
        System.out.println("true");
    } else {
        System.out.println("false");
    }
}

This is returning me : false, true, false, false, false

这是我回:falsetruefalsefalsefalse

My requirement is just to return trueif the acqurierSAis in the propertyfile or else return false, I want only a single value. Currently it is returning me the values in loop.

我的要求是只为了回报true,如果acqurierSA是在propertyfile要不回false,我想只有一个值。目前它正在将循环中的值返回给我。

回答by amicngh

You could make a listform Arrayand then check with contains()

你可以制作一个list表格Array,然后检查contains()

String[] acqurierSystemAlias = properties.getProperty("acqurierSystemAlias").split(",");

List<String> lList=Arrays.asList(acqurierSystemAlias);

boolean found=lList.contains(acqurierSA );
System.out.println(found);

No need to traverse through the array.

无需遍历数组。

回答by assylias

You could use a dedicated variable to do this:

您可以使用专用变量来执行此操作:

boolean found = false;

for(String xyz: acqurierSystemAlias){
    if(xyz.equalsIgnoreCase(acqurierSA)){
        found = true;
        break;
    }
}
System.out.println(found);

回答by andy.wen

maybe you need not split the property just

也许你不需要分割财产

System.out.println(("," + properties.getProperty("acqurierSystemAlias") + ",").contains("," +acqurierSA+ "," ));

回答by NPKR

Check this.

检查这个。

String acqurierSA = "CTC1";
String[] acqurierSystemAlias = properties.getProperty("acqurierSystemAlias").split(",");

List<String> strList = Arrays.asList(acqurierSystemAlias);
strList.contains(acqurierSA);

回答by Prashant Chilwant

public String processMountCheck() {
        logger.info("Logging an INFO-about Directory exists or not.");
        processPropFile();
        String[] urls = prop.getProperty("URL").split(",");
        List<String> list = Arrays.asList(urls);
        String[] folder = prop.getProperty("DIRECTORY").split(",");
        List<String> folders = Arrays.asList(folder);
        for (int i = 0; i < folders.size(); i++) {
            String k = folders.get(i);
            File file = new File(k);
            if (file.isDirectory()) {
                logger.info("Connected to a Directory!"+folders.get(i));
                if (file.list().length > 0) {
                    logger.info("Directory is not empty!");
                } else {
                    logger.info("Directory is empty!");
                }
            } else {
                logger.error("Invalid Directory!");
                // System.out.println("This is not a directory");
            }
        }
        return null;
    }