2012-10-15 106 views
1

Possible Duplicate:
Java Too Many Open Files的Java的BufferedWriter IOException異常「打開的文件太多」

這不是重複,所提到的問題是不同的,只有標題是相同的,請仔細閱讀

這是我的文件寫功能

public static void WriteLog(String LogLine) { 
    String filePath = CommonClass.ReadPropertiesFile("LogFilepath"); 
    BufferedWriter out = null; 
    try { 
     // Create file 
     FileWriter fstream = new FileWriter(filePath, true); 
     out = new BufferedWriter(fstream); 
     out.write(LogLine + "\r\n"); 

    } catch (Exception e) {//Catch exception if any 
     System.err.println("Error: " + e.getMessage()); 
    } finally { 
     //Close the output stream 
     if (out != null) { 
      try { 
       out.write("Closing stream\r\n"); 
       out.close(); 

      } catch (IOException ex) { 
       System.err.println("Error Closing stream: " + ex.getMessage()); 
       Logger.getLogger(LogWritter.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 

我也看到this question但它似乎並沒有幫助,如果近是一個阻塞調用,然後它不應該給這個問題。

但是當我打電話WRITELOG功能在一個循環過於頻繁,即我得到這個錯誤:

Error: (No such file or directory) 
Could not load properties File, Exception: (Too many open files), 
Error: (No such file or directory) 
Could not load properties File, Exception: (Too many open files), 

調用某個特定數量後,在隨後每次調用我不斷獲取此錯誤並沒有更多的文字寫在文件中。任何人都可以告訴我我完全困惑的原因。

在此先感謝

+0

這可能是相關的:http://stackoverflow.com/questions/4289447/java-too-many-open-files –

+2

你有太多打開的文件。該進程在進程啓動時指定了一些最大數量的打開文件,並且您已超出該限制。可能你沒有在某處關閉文件。 –

+0

@熱舔:多數民衆贊成我想問,我在哪裏錯過關閉文件,我想我可以讀取例外說「太多打開的文件」我想知道文件沒有被關閉的地獄 – shabby

回答

2

只讀一次您的屬性文件。

private static String filePath = CommonClass.ReadPropertiesFile("LogFilepath"); 
public static void WriteLog(String LogLine) { 
    ... 

背後CommonClass.ReadPropertiesFile的代碼可能是馬車。

+0

去LastCoder的路!我錯過了這個,我以爲我在關閉日誌文件時遇到問題,謝謝 – shabby

3

看看裏面ReadPropertiesFile()

CommonClass.ReadPropertiesFile("LogFilepath"); 

我想近距離()丟失...

壞:

properties.load(new FileInputStream(PropertiesFilePath)); 

更好:

InputStream is = new FileInputStream(PropertiesFilePath); 
properties.load(is); 
is.close(); 

但AMHO屬性文件可以被讀取一次,每次運行,而不是每次都需要一個屬性值

+0

是的,我如何關閉它?我只是使用properties.load(新FileInputStream(PropertiesFilePath)) – shabby

+0

@shabby - 將其分解爲兩個語句,以便捕獲FileInputStream。 –

0

嘗試關閉文件:

 fstream.close(); 

我和你的代碼進行了測試。

相關問題