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?
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?
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);
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,
Hardcore solution.
Read this file as a text file into a String. See example.
Replace :
with =
.
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.
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();
}