2016-11-30 58 views
0

PropertiesConfiguration.java没有close()方法。还有什么需要做的来释放文件吗?我希望在生产中使用它之前确定。我查看了代码,但没有看到任何内容。我不完全确定PropertiesConfiguration.setProperty()如何在没有打开连接的情况下工作,然后该连接将不得不关闭。Apache Commons Configuration - PropertiesConfiguration closed

+3

你在说什么? – shmosel

+0

JRE中没有'PropertiesConfiguration'类。如果你从其他地方得到它,你需要解释。 – chrylis

+0

我假设他在谈论Apache Commons Configuration中的'PropertiesConfiguration'类。如果您不知道如何使用“加载”方法,则无法明确回答此问题,但作为一般经验法则,如果您打开资源,_you_需要关闭它。如果数千字库使用的库打开它,您可以放心,它正在被正确清理。 – rmlan

回答

1

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),其调用其超级构造:

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语句中,可以看到,InputStream为在任何情况下关闭。

+0

谢谢@davidxxx。我想我不相信我所看到的。 – pyetti

相关问题