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/