Java Properties Save saveProperty(String key, String value)

Here you can find the source of saveProperty(String key, String value)

Description

save Property

License

Apache License

Declaration

public static void saveProperty(String key, String value) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;
import java.util.Properties;

public class Main {
    private static final String FILE_NAME = "config.properties";
    private static Properties mProperties;

    public static void saveProperty(String key, String value) throws IOException {
        if (mProperties == null)
            mProperties = new Properties();

        createFile();/*from w  w  w  .  jav a  2 s . co m*/

        try (OutputStream lOutputStream = new FileOutputStream(FILE_NAME)) {
            mProperties.put(key, value);
            mProperties.store(lOutputStream, null);
        } catch (IOException e) {
            throw e;
        }
    }

    private static void createFile() throws IOException {
        File lFile = new File(FILE_NAME);
        if (!lFile.exists()) {
            lFile.getParentFile().mkdirs();
            lFile.createNewFile();
        }
    }
}

Related

  1. saveProperties(String fileName, Properties properties, String title)
  2. saveProperties(String propName, Properties props)
  3. savePropertiesToEncodedString(Properties props, String comment)
  4. savePropertiesToString(Properties props, String comment)
  5. saveProperty(String key, String value)
  6. saveProperty(String key, String value)
  7. saveProps(Properties p, String fname, String comment)
  8. saveProps(String path, Properties props)
  9. saveSorted(Properties props, File file)