Created
December 20, 2020 16:06
-
-
Save eliasnogueira/df8edc58c792d176c1f0c730cd73f054 to your computer and use it in GitHub Desktop.
Example of Properties file read in Java with the Singleton pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReadProperties { | |
private static final Logger LOG = LoggerFactory.getLogger(ReadProperties.class); | |
private static ReadProperties instance = null; | |
private Properties properties = null; | |
private ReadProperties() { | |
properties = new Properties(); | |
try { | |
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("general.properties"); | |
properties.load(inputStream); | |
} catch (IOException e) { | |
LOG.error(e.getMessage()); | |
} | |
} | |
public static synchronized ReadProperties getInstance() { | |
if (instance == null) { | |
instance = new ReadProperties(); | |
} | |
return instance; | |
} | |
public String getValue(String key) { | |
return this.properties.getProperty(key, String.format("The key %s does not exists!", key)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment