I’m building a ResourceBundle from a file, this bundle holds < String, String> values.
InputStream in = getClass().getResourceAsStream('SQL.properties'); properties = new PropertyResourceBundle(in); in.close();
I would like to add/replace on this bundle some properties that I’m passing from the command line using -Dsome.option.val.NAME1=HiEarth
I don’t care dumping the old bundle and creating a new one instead.
Could you please tip?
I think that what I need to do is :
- Create from the bundle a HashMap< String, String>
- Replace values.
- Transform the HashMap into a InputStream. //This is the complicated part…
- Build the new bundle from that.
This does some of what you want (converts the System.properties to a ResourceBundle). Better error handling is left up to you 🙂
public static ResourceBundle createBundle() { final ResourceBundle bundle; final Properties properties; final CharArrayWriter charWriter; final PrintWriter printWriter; final CharArrayReader charReader; charWriter = new CharArrayWriter(); printWriter = new PrintWriter(charWriter); properties = System.getProperties(); properties.list(printWriter); charReader = new CharArrayReader(charWriter.toCharArray()); try { bundle = new PropertyResourceBundle(charReader); return (bundle); } catch(final IOException ex) { // cannot happen ex.printStackTrace(); } throw new Error(); }