Skip to content

Instantly share code, notes, and snippets.

@eliasnogueira
Created December 20, 2020 16:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasnogueira/df8edc58c792d176c1f0c730cd73f054 to your computer and use it in GitHub Desktop.
Save eliasnogueira/df8edc58c792d176c1f0c730cd73f054 to your computer and use it in GitHub Desktop.
Example of Properties file read in Java with the Singleton pattern
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