I've many java properties files containing credentials and other configurations, and I'm looking for the most compact code to load these in both Nashorn's javascript and Java.
So far, I'm left with 2 lines:
credentials = new java.util.Properties();
credentials.load(new java.io.FileInputStream("credentials.properties"));
I've tried this obvious merge but it does not work in Nashorn:
credentials = (new java.util.Properties()).load(new java.io.FileInputStream("credentials.properties"));
Can these be merged as a one-liner? It does not matter if readability is deeply affected.
If you want to do it Java-8 style with a very inconventional use of Optional
and lambdas, you can try this
Properties credentials=Optional.of(new Properties()).map(p->{try{p.load(new FileInputStream("credentials.properties"));}catch(IOException e){}return p;}).get();
(Longer, but more readable version with newlines)
Properties credentials = Optional.of(new Properties())
.map(p -> {
try
{
p.load(new FileInputStream("credentials.properties"));
}
catch (IOException ex) {}
return p;
})
.get();
Updates:
without the try/catch it becomes this for java:
Properties credentials = Optional.of(new Properties()).map(p -> { p.load(new FileInputStream("credentials.properties")); return p; }).get();
and for Nashorn js:
var credentials = java.util.Optional.of(new java.util.Properties()).map(function(p) { p.load(new java.io.FileInputStream("credentials.properties")); return p;}).get();