User Tools

Site Tools


java:properties:avoid-escaping-characters

Avoid escaping characters in java Properties class

After calling the store method, all the special characters from the key values will be escaped by \
Ex: instead of :test= we'll have in the properties file \:test\=
To avoid this we need to override the store method:

Properties properties = new Properties(){
	@Override
	public void store(OutputStream out, String comments) throws IOException {
		BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(out, System.getProperty("file.encoding")));
		synchronized (this) {
			for (Enumeration e = keys(); e.hasMoreElements();) {
				String key = (String) e.nextElement();
				String val = (String) get(key);
				bufferedWriter.write(key + "=" + val);
				bufferedWriter.newLine();
			}
		}
		bufferedWriter.flush();
	}
};
java/properties/avoid-escaping-characters.txt · Last modified: 2023/07/04 19:36 by 127.0.0.1