无法使用getResourceAsStream获取属性文件 - 空值

时间:2014-09-01 14:10:45

标签: java stream resources getresource

我正在尝试从jar获取文件并返回null。 jar文件的目录结构如下所示(core-1.0.jar是我提取的内容);

enter image description here

我使用以下代码尝试阅读swift.properties文件

/**
 * Get the relevant database connection properties
 * @param path the path to the properties file
 * @return return the loaded properties file
 */
public static Properties getDatabaseConnectionProps(ApplicationName appName) throws IOException{

    if(appName == null){
        throw new IllegalArgumentException("Path to proeprties file was null or empty");
    }

    Properties props = null;

    try(InputStream resourceStream = DatabaseUtilities.class.getResourceAsStream(appName.getValue())) {
        if(resourceStream != null){
            props = new Properties();
            props.load(resourceStream);
            return props;
        }else{
            throw new IllegalArgumentException("Invalid properties file path was provided");
        }
    } catch (IOException e) {
        throw e;
    }
}

appName.getValue()是" /swifte.properties" 。它是导致错误的主要斜线吗?它不应该表明我们想要从jar的根目录中查看吗?

由于

0 个答案:

没有答案