java - Properties File multi-line values using PropertiesConfiguration -


so far, have project read in properties file using propertiesconfiguration (from apache), edit values edit, , save change file. keeps comments , formatting , such, 1 thing change taking multi-line values formatted this:

key=value1,\     value2,\     value3 

and turns array style:

key=value1,value2,value3 

i able print lines formatted before.
did via method:

propertiesconfiguration config = new propertiesconfiguration(configfile); config.setproperty(key,value); config.save(); 

i created work around in case else needs functionality. also, there better way this, solution works me.

first, set propertiesconfiguration delimiter new line character so:

propertiesconfiguration config = new propertiesconfiguration(configfile); config.setlistdelimiter('\n'); 

then need iterate through , update properties (to set format):

iterator<string> keys = config.getkeys();     while (keys.hasnext()) {         string key = keys.next();          config.setproperty(key,setpropertyformatter(key, config.getproperty(key))) ;      } 

use method format value list data (as shown above):

private list<string> setpropertyformatter(string key, object list) {     list<string> tempproperties = new arraylist<>();     iterator<?> propertyiterator = propertyconverter.toiterator(list, '\n');;     string indent = new string(new char[key.length() + 1]).replace('\0', ' ');      boolean firstiteration = true;     while (propertyiterator.hasnext()) {         string value = propertyiterator.next().tostring();          boolean lastiteration = !propertyiterator.hasnext();          if(firstiteration && lastiteration) {             tempproperties.add(value);             continue;         }          if(firstiteration) {             tempproperties.add(value + ",\\");             firstiteration = false;             continue;         }          if (lastiteration) {              tempproperties.add(indent + value);             continue;         }           tempproperties.add(indent + value + ",\\");     }        return tempproperties; } 

then going correct, except save function takes double backslash stored in list, , turns 4 slashes in file! need replace single backslash. did so:

try {         config.save(new file(filepath));           byte[] readin = files.readallbytes(paths.get(filepath));         string replacer = new string(readin, standardcharsets.utf_8).replace("\\\\\\\\", "\\");          bufferedwriter bw = new bufferedwriter(new outputstreamwriter(new fileoutputstream(filepath, false), "utf-8"));         bw.write(replacer);         bw.close();  } catch (configurationexception | ioexception e) {         e.printstacktrace(); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -