Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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