Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Properties;
- class PropertiesExample {
- protected String pathProperties;
- protected Properties properties;
- public PropertiesExample(String path) {
- pathProperties = path;
- properties = new Properties();
- try {
- FileInputStream stream = new FileInputStream(path);
- properties.load(stream);
- stream.close();
- } catch (FileNotFoundException ex) {
- // log warning
- } catch (IOException ex) {
- // log warning
- }
- }
- protected <T> T getProperty(String key, T fallback) {
- String value = properties.getProperty(key);
- if (value == null) {
- return fallback;
- } else {
- return new T(value);
- }
- }
- protected void setProperty(String key, Object value) {
- properties.setProperty(key, value.toString());
- try {
- FileOutputStream stream = new FileOutputStream(pathProperties, false);
- properties.store(stream, "comment");
- stream.close();
- } catch (FileNotFoundException ex) {
- // log warning
- } catch (IOException ex) {
- // log warning
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement