PropertiesConfiguration

PropertiesConfiguration

PropertiesConfiguration.java没有close()方法。要释放文件,还需要执行其他操作吗?在生产中使用此产品之前,我想确定一下。我已经看过代码,但是什么也看不到。我不完全确定PropertiesConfiguration.setProperty()的工作方式,而无需打开与文件的连接,然后必须将其关闭。

最佳答案

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

您可以在PropertiesConfigurationvoid load(URL url)方法中进行确认。

这是这种方法的调用方式:

1)调用org.apache.commons.configuration.AbstractFileConfiguration构造函数:

public PropertiesConfiguration(File file) throws ConfigurationException


2)调用其超级构造函数:

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)调用PropertiesConfiguration

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


4)调用load()

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(String fileName)

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);
        }
    }
}


并且在load(URL url)语句中,您可以看到在任何情况下inputstream都是关闭的。

10-08 01:19