首页 文章

Groovy:如何加载属性文件并处理“找不到文件”的情况?

提问于
浏览
0

我使用以下代码加载属性文件:

File propertiesFile = new File(PROPS_FILE_PATH)
    Properties workflowProperties = new Properties()
    propertiesFile.withInputStream {
        workflowProperties.load it
    }

等等

这很好用 . 但是当文件不存在时,我无法处理这种情况 . 这样做的正确方法是什么?捕获FileNotFoundException或任何其他异常对我不起作用 .

谢谢 .

1 回答

  • 0

    你应该检查文件是否存在:

    File propertiesFile = new File(PROPS_FILE_PATH)
    if( !propertiesFile.exists() ) throw new Exception( 'File not found' ) // bail out
    

相关问题