java - Apache Commons 配置 - PropertiesConfiguration 已关闭

标签 java io properties-file

PropertiesConfiguration.java 没有 close() 方法。是否还需要执行其他操作才能释放该文件?我想在生产中使用它之前确定一下。我查看了代码,但没有看到任何内容。我不完全确定 PropertiesConfiguration.setProperty() 在不打开与文件的连接(然后必须关闭该连接)的情况下如何工作。

最佳答案

org.apache.commons.configuration.PropertiesConfiguration当属性加载到 PropertiesConfiguration 中时,输入(流、路径、url 等)当然会关闭。实例。

您可以在 void load(URL url) 中得到确认方法org.apache.commons.configuration.AbstractFileConfiguration .

此方法的调用方式如下:

1) PropertiesConfiguration调用构造函数:

public PropertiesConfiguration(File file) throws ConfigurationException

2) 调用其 super 构造函数:

public AbstractFileConfiguration(File file) throws ConfigurationException
{
    this();

    // set the file and update the url, the base path and the file name
    setFile(file);

    // load the file
    if (file.exists())
    {
        load(); // method which interest you
    }
}

3) 调用 load() :

public void load() throws ConfigurationException
{
    if (sourceURL != null)
    {
        load(sourceURL);
    }
    else
    {
        load(getFileName());
    }
}

4) 调用 load(String fileName) :

public void load(String fileName) throws ConfigurationException
{
    try
    {
        URL url = ConfigurationUtils.locate(this.fileSystem, basePath, fileName);

        if (url == null)
        {
            throw new ConfigurationException("Cannot locate configuration source " + fileName);
        }
        load(url);
    }
    catch (ConfigurationException e)
    {
        throw e;
    }
    catch (Exception e)
    {
        throw new ConfigurationException("Unable to load the configuration file " + fileName, e);
    }
}

5) 调用 load(URL url)

public void load(URL url) throws ConfigurationException
{
    if (sourceURL == null)
    {
        if (StringUtils.isEmpty(getBasePath()))
        {
            // ensure that we have a valid base path
            setBasePath(url.toString());
        }
        sourceURL = url;
    }

    InputStream in = null;

    try
    {
        in = fileSystem.getInputStream(url);
        load(in);
    }
    catch (ConfigurationException e)
    {
        throw e;
    }
    catch (Exception e)
    {
        throw new ConfigurationException("Unable to load the configuration from the URL " + url, e);
    }
    finally
    {
        // close the input stream
        try
        {
            if (in != null)
            {
                in.close();
            }
        }
        catch (IOException e)
        {
            getLogger().warn("Could not close input stream", e);
        }
    }
}

finally中语句,您可以看到输入流在任何情况下都是关闭的。

关于java - Apache Commons 配置 - PropertiesConfiguration 已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40897451/

相关文章:

java - Minmax 算法不返回根的直接子代(返回非法移动)

c++ - C 中优雅的二进制 i/o?

Java - XStream 关​​闭与文件的连接

java - 如何将字段名称传递给属性文件中的全局消息

java - Maven 属性插件

java - CardLayout 中显示错误的 JPanel。 getGraphics() 的问题

Java字符串数组双引号错误

java - 如何将 MVC 应用于多个窗口/对话框

java - 从文件读取,内存不足

java - 正在添加但未使用 Spring Boot : external messages. 属性