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

9
  • This is probably related: stackoverflow.com/questions/4289447/java-too-many-open-files Commented Oct 15, 2012 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. Commented Oct 15, 2012 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 Commented Oct 15, 2012 at 19:04
  • 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().) Commented Oct 15, 2012 at 19:12
  • 1
    Surely the exception occurs in new FileWriter(), not new BufferedWriter()? Commented Oct 15, 2012 at 20:35

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

yeah it is, how do i close it? i am just using properties.load(new FileInputStream(PropertiesFilePath))
@shabby -- Break that into two statements, so you capture the FileInputStream.
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.

1 Comment

Way to go LastCoder! I was missing this, I thought i am having problem in closing the log file, thanks
0

Try closing the file:

        fstream.close();

I've tested it with your code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.