1

Possible Duplicate:
Java Too Many Open Files

This is not a duplicate, the referred question is different, only the title is same, please read carefully

This is my file write function

 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);
            }
        }
    }
}

i have also seen this question but it doesn't seem to help, if close is a blocking call then it shouldn't give this problem.

but when i call WriteLog function too frequently i.e. in a loop i get this error:

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), 

After some specific number of calls, on every subsequent call i keep on getting this error and no more text is written in the file. Can anybody tell me the reason I am totally confused.

Thanks in advance

Community
  • 1
  • 1
shabby
  • 3,002
  • 3
  • 39
  • 59
  • This is probably related: http://stackoverflow.com/questions/4289447/java-too-many-open-files – Roddy of the Frozen Peas Oct 15 '12 at 19:02
  • 2
    You've got too many open files. The process has some max number of open files that is specified when the process is started, and you've exceeded that limit. Probably you're failing to close files somewhere. – Hot Licks Oct 15 '12 at 19:02
  • @Hot Licks: thats what i am trying to ask, where am i missing to close the file, I think i can read the exception saying "Too many open files" I want to know where the hell the file is not being closed – shabby Oct 15 '12 at 19:04
  • If an exception occurs on `out = new BufferedWriter(fstream)`, you never close `fstream`. – Roddy of the Frozen Peas Oct 15 '12 at 19:05
  • Look at where the exception is being raised. Odds are that's where the files are getting opened that aren't being closed. – Hot Licks Oct 15 '12 at 19:05
  • @RoddyoftheFrozenPeas but isnt this line out = new BufferedWriter(fstream) inside the try block, if exception occurs here, wouldnt the finallly block execute and close the stream?? – shabby Oct 15 '12 at 19:07
  • 1
    @Shabby. No, you close `out` if `out != null`. If the exception is raised on `out = ...` then `out = null` and `fstream` is already open and is never closed. (Otherwise `fstream` gets closed by `out.close()`.) – Roddy of the Frozen Peas Oct 15 '12 at 19:12
  • There is a problem in Java that the finalizer mechanism, which is supposed to automatically close unreferenced files, has always been buggy (and in fact in later releases they just about gave up on it). Plus, the finalizer mechanism only runs when GC runs, and you can easily run out of files between GCs. So try closing, eg, the file you open in your ReadProperties. – Hot Licks Oct 15 '12 at 19:13
  • 1
    Surely the exception occurs in new FileWriter(), not new BufferedWriter()? – user207421 Oct 15 '12 at 20:35

3 Answers3

3

Take a look inside ReadPropertiesFile()

CommonClass.ReadPropertiesFile("LogFilepath");

I suppose a close() is missing...

Bad:

properties.load( new FileInputStream( PropertiesFilePath ));

Better:

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

But AMHO a PropertiesFile may be read once per run, not each time a property value is needed

Aubin
  • 14,617
  • 9
  • 61
  • 84
2

Just read your properties file once.

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

The code behind CommonClass.ReadPropertiesFile is likely buggy.

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
  • Way to go LastCoder! I was missing this, I thought i am having problem in closing the log file, thanks – shabby Oct 15 '12 at 19:17
0

Try closing the file:

        fstream.close();

I've tested it with your code.

PbxMan
  • 7,525
  • 1
  • 36
  • 40