从名称对为

时间:2017-02-17 15:00:00

标签: java list file

public String readvaluesFromFile(String PassingAnameString) {
    File myfile = new File("src/main/resources/file.txt");
    FileInputStream myFileInput = new FileInputStream(errorFile);
    Properties properties = new Properties();
    properties.load(myFileInput);
    myFileInput.close();
    Enumeration<Object> filenameKeys = properties.keys();
    while (filenameKeys.hasMoreElements()) {
        String fileValues = properties.getProperty(PassingAnameString);
        return "Reference Name: " + filenameKeys + " : " + fileValues;
    }
    return "There was a problem.";
}

我有一个函数将一串名称传递给一个函数    从文本文件中返回这些名称的相应值。

例如。文本文件包含:name = value name = value name = value

函数1将一个“name,name”字符串传递给函数2,该函数读取    文件。我无法弄清楚如何让文件读取    多个名称并返回相应的值。有什么输入吗?

上面的功能2

1 个答案:

答案 0 :(得分:0)

接受可变数量的字符串并返回相等数量的字符串值。   试试这个:

public String[] readvaluesFromFile(String... PassingManyStrings) {
    File myfile = new File("src/main/resources/file.txt");
    FileInputStream myFileInput = new FileInputStream(errorFile);
    Properties properties = new Properties();
    properties.load(myFileInput);
    myFileInput.close();

    String[] toReturn = new String[ PassingManyStrings.length ];
    int i=0;
    for(String oneProp: PassingManyStrings){
        toReturn[i++] = properties.getProperty(oneProp);
        if( toReturn[i++] == null)
              toReturn[i++] = "There was a problem.";
        else
              toReturn[i++] = "Reference Name: " + oneProp + " : " + toReturn[i++];
    }
    return toReturn;
}