我们有这样的代码:
String tempDir = SwingInstanceManager.getInstance().getTempFolderPath(clientId);
if (tempDir == null) {
tempDir = System.getProperty(Constants.TEMP_DIR_PATH);
if (tempDir == null) {
tempDir = new File(System.getProperty("java.io.tmpdir")).toURI().toString();
}
}
我想删除方括号,所以如果它只有2个值,我会这样写:
String tempDir = Optional.ofNullable(SwingInstanceManager.getInstance().getTempFolderPath(clientId)).orElse(System.getProperty(Constants.TEMP_DIR_PATH));
但是有什么方法可以为3个以上的值编写这样的链吗?(在orElse调用中使用第二个可选方法取消)
由于您的第二个选项实际上是一个属性,因此您可以依赖于该getProperty(String, String)
方法而不仅仅是getProperty(String)
:
String tempDir = Optional.ofNullable(SwingInstanceManager.getInstance().getTempFolderPath(clientId))
.orElse(System.getProperty(Constants.TEMP_DIR_PATH,
new File(System.getProperty("java.io.tmpdir")).toURI().toString());
尽管我建议Path
不要File
在后面的部分(Paths.get(System.getProperty("java.io.tmpdir")).toURI().toString()
)中使用,
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句