Code Snippet

Just another Code Snippet site

[Java] Read/Write properties file (keeping layout & comments)

@Service
public class ConfigurationService {

    /** Logger. */
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationService.class);

    /** Configuration Layout. */
    private PropertiesConfigurationLayout configPropsLayout;

    /** Properties. */
    private PropertiesConfiguration configProps;

    /** Properties file encoding. */
    private final String configFileCharset = "UTF-8";

    /** Properties file path. */
    private final String configFileResource = "/config/config.properties";

    /**
     * Read configuration file (keeping layout & comments).
     */
    public void loadConfigurationFile() {
        LOGGER.debug("Loading the configuration...");
        int nbElements = 0;

        // Read Property File
        final Reader reader =
                new InputStreamReader(Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream(configFileResource));

        configProps = new PropertiesConfiguration();
        configPropsLayout = new PropertiesConfigurationLayout(configProps);

        try {
            configPropsLayout.load(reader);

            // Read configuration properties
            int confProp = configPropsLayout.getConfiguration().getInt("test");

            if (configPropsLayout.getKeys() != null) {
                nbElements = configPropsLayout.getKeys().size();
            }

        } catch (org.apache.commons.configuration.ConfigurationException e) {
            LOGGER.error("<loadConfiguration> : load", e);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                LOGGER.error("<loadConfiguration> : close", e);
            }
        }

        LOGGER.info("{} element(s) loaded", nbElements);
    }

    /**
     * Update configuration file.
     */
    public void writeConfiguration() {

        // Update values
        configPropsLayout.getConfiguration().setProperty("test", 0);

        Writer configFileWriter = null;
        
        try {
            final URL configFileUrl = Thread.currentThread().getContextClassLoader().getResource(configFileResource);
            final String configFilePath = URLDecoder.decode(configFileUrl.getPath(), configFileCharset);
            LOGGER.debug("Saving configuration to " + configFilePath);
            final File configFile = new File(configFilePath);
            configFileWriter = new FileWriter(configFile);

            // Store configuration to file
            configPropsLayout.save(configFileWriter);

            // Reload configuration after modifications
            this.loadConfigurationFile();

        } catch (UnsupportedEncodingException e) {
            // TODO : handle exception
        } catch (IOException e) {
            // TODO : handle exception
        } catch (ConfigurationException e) {
            // TODO : handle exception
        } finally {
            try {
                configFileWriter.close();
            } catch (IOException e) {
                // TODO : handle exception
            }
        }
    }
}

,


Leave a Reply

Your email address will not be published. Required fields are marked *