2016-11-30 88 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

相關問題