0

I have properties file which contains key:value list such as follow:

key1 : value1
key2 : value2

how can I read and write this file with Properties class? It's possible or not?

M2E67
  • 818
  • 7
  • 21

4 Answers4

1

I look java Properties class(in jdk) and found that java use constant '=' for saving properties file by following method:

private void store0(BufferedWriter bw, String comments, boolean escUnicode)
    throws IOException
{
    if (comments != null) {
        writeComments(bw, comments);
    }
    bw.write("#" + new Date().toString());
    bw.newLine();
    synchronized (this) {
        for (Enumeration<?> e = keys(); e.hasMoreElements();) {
            String key = (String)e.nextElement();
            String val = (String)get(key);
            key = saveConvert(key, true, escUnicode);
            /* No need to escape embedded and trailing spaces for value, hence
             * pass false to flag.
             */
            val = saveConvert(val, false, escUnicode);
            bw.write(key + "=" + val);
            bw.newLine();
        }
    }
    bw.flush();
}

so, it is not possible to handle my properties file. but for erad/write my properties file, I found that this file is a yaml foramt and we can read file by this method easily:

public Map read(String path) {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    try {
        File file = new File(path);
        Map map = mapper.readValue(file, Map.class);
        return map;
    } catch (Exception e) {
        logger.error("Error during read Yml file {}", path, e);
    }
    return new HashMap();
}

and update file by following method:

public boolean update(String path, Map content) {
    try {
        YAMLFactory yamlFactory = new YAMLFactory();
        yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);

        File file = new File(path);
        ObjectMapper mapper = new ObjectMapper(yamlFactory);
        ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
        writer.writeValue(file, content);
        return true;
    } catch (Exception e) {
        logger.error("Error during save Yml file {}", path, e);
    }
    return false;
}

it si import that I use following code to prevent using double quotes for values:

yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
M2E67
  • 818
  • 7
  • 21
0

Yes. Have you considered trying it? or consulting the documentation?

The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,

user207421
  • 289,834
  • 37
  • 266
  • 440
0

Hardcore solution.

  1. Read this file as a text file into a String. See example.

  2. Replace : with =.

  3. Parse properties from String, see Parsing string as properties

    public Properties parsePropertiesString(String s) {
        final Properties p = new Properties();
        p.load(new StringReader(s));
        return p;
    }
    

And if you want to write properties to file, do the same in the reversed order. Write properties into a String, replace = with :, and write the string into the file.

Yan Khonski
  • 9,178
  • 13
  • 52
  • 88
  • it is not fine solution; I debug and searching in java Properties class and found that java using '=' constant for write key=value. – M2E67 Feb 28 '18 at 10:07
  • @M2E67 You should probably add that to your question, since you do not say you have problem with `=` being written (Yes it will not be `:` but from other answers it seems properties can use both `=` and `:` so it is your additional requirement) – Piro says Reinstate Monica Feb 28 '18 at 10:17
0

You can use below code to read a properties file. Please note ':' will work same as '='.

try (InputStream in = new FileInputStream("filename.propeties")) {
    Properties prop = new Properties();
    prop.load(in);


    for (String key: prop.stringPropertyNames()) {
        String value = prop.getProperty(key);
        System.out.println(key+ "=" + value);
    }
} catch (IOException e) {
    e.printStackTrace();
}
kryger
  • 11,746
  • 8
  • 41
  • 60
DhaRmvEEr siNgh
  • 1,393
  • 2
  • 11
  • 17