Properties problem with the ":" symbol. There is an ini file, it is edited by a person, closes, saves.

Ini example:

 connects=streamServer://null module.streamServer://null.run=2 

Next, a person starts the program, the program should read this file, extract connects from it, build a line on it ( module.ЗНАЧЕНИЕ ИЗ CONNECTS.commands ) and get the value from the ini file using this line, plus this value +1 and write 3 ini . Everything seems to be normal, but only the place of a readable view has to be done like this module.streamServer\://null.commands and when you save connects=streamServer://null => connects=streamServer\://null how to teach Properties to deal with the symbol ":" , or suggest a semblance of Properties .

    2 answers 2

    The colon is the same legal key and value separator as the = sign is written about in the documentation . Who met earlier in the line - he divided.

    So, if you want to use .propeties files by standard means, you will have to either give up the colons in the keys or escape them with a backslash.

    The alternative is to write your own class, which will manually read the file, do String.split("=") line by line, shift the two halves to the Map , and when the time comes, write the contents of the Map back to the file, sticking the key with the value = .

    • I think this is the only option - Denis Kotlyarov

    And you do not learn anything. The class Properties will do everything for you. As an example, run the code below.

      Properties p = new Properties(); p.put("asd", "one:targer"); FileWriter w = new FileWriter(new File("config.ini")); p.store(w, "blabla"); FileReader reader = new FileReader(new File("config.ini")); p.load(reader); System.out.println(p.get("asd")); 

    After executing this code, you will receive a config.ini file containing

     #blabla #Sun Mar 20 23:44:00 EET 2016 asd=one/:targer 

    then reading from this file will return the line:

     one:targer 

    and it is not necessary to oblige the user to add / to the config file, because it lacks the Properties class to handle. The reason is that when reading from a file, the key is considered the part of the line up to the first unshielded character / or: the rest is the value.

    Well, lastly, I will mention the ini4j library , which is essentially designed to communicate more closely with .ini files. Good luck