Apache Commons配置 - PropertiesConfiguration关闭

时间:2016-11-30 20:21:18

标签: java io properties-file

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

1 个答案:

答案 0 :(得分:1)

org.apache.commons.configuration.PropertiesConfiguration中,当PropertiesConfiguration实例中加载属性时,输入(stream,path,url等)当然会关闭。

您可以使用void load(URL url)的{​​{1}}方法进行确认。

以下是调用此方法的方法:

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

PropertiesConfiguration

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

public PropertiesConfiguration(File file) throws ConfigurationException

3)调用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 } }

load()

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

load(String fileName)

5)调用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); } }

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); } } } 语句中,您可以看到输入流在任何情况下都是关闭的。