How to read a value from a properties file of type enum?

I have an enumeration as shown below:

public enum EnvironmentType {PRODUCTION, TEST, DEVELOPMENT}

in the properties file, the key value looks like:

app.environmentType = TEST

we know that when I read a value from a properties file using a key, it returns as String as

String envType = properties.getProperty("app.environmentType");

My requirement:

EnvironmentType envType = EnvironmentType.TEST;

Now I want to know that there is a way to get the type value enum? how can it be parseor cast?

+5
source share
2 answers
EnvironmentType envType =   EnvironmentType.valueOf(envTypeString);
+3
source

You can use the method valueOfas follows:

String envTypeStr = properties.getProperty("app.environmentType");
EnvironmentType envType = EnvironmentType.valueOf(envTypeStr);

valueOf() values() . java-. , . SortOrder Normalizer.Form

+3

All Articles