';试试catch';从Java1.7到Java1.6的重构

';试试catch';从Java1.7到Java1.6的重构,java,try-catch,Java,Try Catch,我使用的是来自其他人的jar文件,但我需要将源代码添加到我的项目中,并在导入此类包时编译它们。问题是这个jar文件似乎是由java1.7生成的,因此它使用了一些java1.7特性。但是我使用的是java1.6。对于此源代码: public Properties getDefaults() { try (InputStream stream = getClass().getResourceAsStream(PROPERTY_FILE)) { Pro

我使用的是来自其他人的jar文件,但我需要将源代码添加到我的项目中,并在导入此类包时编译它们。问题是这个jar文件似乎是由
java1.7
生成的,因此它使用了一些
java1.7
特性。但是我使用的是
java1.6
。对于此源代码:

public Properties getDefaults() {
    try (InputStream stream
            = getClass().getResourceAsStream(PROPERTY_FILE)) {

        Properties properties = new Properties();
        properties.load(stream);
        return properties;

    } catch (IOException e) {
        throw new RuntimeException(e);

    }
}
eclipse给出了这样的错误提示:

Resource specification not allowed here for source level below 1.7

那么我如何重写这样的代码,以便它可以被
java1.6
处理?

要重新编写与Java 1.6兼容的try-with-resource语句,请执行以下操作:

  • try
    块开始之前声明变量
  • try
    块的顶部创建变量
  • 添加一个将关闭资源的
    finally
例如:

InputStream stream = null;
try
{
    stream = getClass().getResourceAsStream(PROPERTY_FILE));
    // Rest of try block is the same
}
// catch block is the same
finally
{
    if (stream != null)
    {
        try {
            stream.close();
        } catch (IOException ignored) { }
    }
}

要重新编写与Java 1.6兼容的try-with-resource语句,请执行以下操作:

  • try
    块开始之前声明变量
  • try
    块的顶部创建变量
  • 添加一个将关闭资源的
    finally
例如:

InputStream stream = null;
try
{
    stream = getClass().getResourceAsStream(PROPERTY_FILE));
    // Rest of try block is the same
}
// catch block is the same
finally
{
    if (stream != null)
    {
        try {
            stream.close();
        } catch (IOException ignored) { }
    }
}

您必须返回到使用好的ol'
最后
块。您能具体说明一下吗?最好显示一些代码。您必须返回到使用好的ol'
finally
块。您能具体说明一下吗?最好显示一些代码。