I am trying to reformat the output of a LinkedHashMap to exclude the commas and curly braces.
Here is what I have for my puts:
token.put("[Server.Protocol]", url.getProtocol() + "\n");
token.put("[Server.hostName]", url.getHost() + "\n");
token.put("[Server.Port]", url.getPort() + "\n");
values.add(token.toString());
token is my LinkedHashMap and values is my LinkedList.
Here is my output:
{[Server.Protocol]=https
, [Server.Name]=myserver
, [Server.Port.HTTPS]=123
}
Here is what I want:
[Server.Protocol]=https
[Server.hostName]=myserver
[Server.Port]=123
Here is my print method:
private void webconfigToINI() throws IOException {
File fout = new File(propFile);
try {
FileOutputStream fos = new FileOutputStream(fout, true /* append */);
writer = new PrintWriter(fos);
if (fos.getChannel().size() == 0)
writer.print("[Code]\n");
for (String value : values) {
//writer.print(value.substring(1, value.length()-1));
writer.print(value);
}
// writer.print("[Code]\n");
writer.flush();
writer.close();
fos.close();
} catch (Exception e) {
throw e;
}
}
Instead of calling toString
on your map you can use this:
String string = token.entrySet()
.stream()
.map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.joining());
If you can't use Java 8 then you have to iterate and joining your data retrieving them from the map using entrySet()
, as the following code:
String string = "";
for (Map.Entry<String, String> value: token.entrySet()) {
string = string + value.getKey() + "="+value.getValue();
}