Save key with various values in Properties file

Asked

Viewed 535 times

3

I have a file .properties where I carry the name of the tests that will be deleted from my application. This file needs to have the same name as the "variables" since they all mean the same thing (deleted tests), the problem is that my code is just recognizing the last test (the NATONE in case).

data properties.:

prop.teste.excl = CQT-SUST
prop.teste.excl = NATONE

code:

public static Properties getProp() throws IOException 
{
    Properties props = new Properties();
    FileInputStream file = new FileInputStream("src/resources/dados.properties");
    props.load(file);
    return props;

}

public ArrayList<String> testeInterno() throws IOException 
{
    String property;
    ArrayList<String> testeInt = new ArrayList<String>();

    Properties prop = getProp();

        for ( int i = 0; i < prop.size(); i++)
        {
            property = "prop.teste.excl"; 
            testeInt.add(i, prop.getProperty(property));

            System.out.println(testeInt.get(i));
        }

    return testeInt;
}

I thought to put ";" at the end of each line of my file but I do not know how to make the code that recognizes ";" as the end of the line and also do not know if it would work.

  • You can add the question which error message occurs?

  • does not show an error, but it fails to catch the first test (CQT-SUST) only returns the (NATONE)

  • Changing the sequence, placing the (CQT-SUST) last it returns from there?

  • yes, he was only taking the last one. But I’ve already solved with Articuno’s answer

1 answer

3


You can insert them into the same property key by separating by comma, and then you treat the return as String, separating the values with the method split, passing the comma as separator:

Example:

prop.teste.excl = CQT-SUST,NATONE

And when it’s time to treat:

String[] array = prop.getProperty("prop.teste.excl").split(",");

This way, you will have an array with the key values "prop.teste.excl", which have been separated by comma.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.