• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.io.FileNotFoundException ( Too many open files )

 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Morning all!
My appliaction some times throws the following error

java.io.FileNotFoundException F:\.."Path"..\<File Name> (Too many open files)

The error is thrown through the following code

public void writeContents() throws IOException, SecurityException {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(absPathOfInputFile));
for (int iCount = 0; iCount < vecFileContents.size(); iCount++) {
bw.write(vecFileContents.elementAt(iCount).toString());
bw.newLine();
}
bw.flush();
bw.close();
}

The application is able to create the file but not able to write into it. Also when i manually try to delete the created file it wont allow me to do that as the file is in use.

I have observed that same error is thrown while system is trying to read from some other file though the file is present and available for reading.

Can anybody explain what could be the problem
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number of files that can be in open state at any point of time is specific to the OS (offcourse ,Can be configured) and you have reached the upper limit of that.Look in the code base of your application ,whehter you have some code that is trying to open a file but not closing the stream after its use.Check for such codes.
 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few places in the code where stream is opened for reading and not closed. But can one or two methods make a diffrence or there is somehing else that is causing the problem.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Opening and not closing a file, even if it's only for reading, can certainly cause this problem.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the best of my knowledge , the descriptor table is shared by files , sockets etc.So if you have too many sockets open , that might also cause.But this varries from OS to OS.Some OS might choose to maintain seperate tables for different descriptors.Different table for file descriptor and different table for sockets.
 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way i can find out number of open files or sockets in the system or how to configure the no. of open files or sockets the system can handle??

I know this is getting away from java but if some one can help me solve my problem i would be really gartefull!

Thanks
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use ulimit -a command in UNIX for getting information about the nofiles (descriptor) parameter and can increase the value also , but I really do not think that this is the solution for this problem.You should try to look for fault code in you application only.
 
Nicole Lacoste
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I find it is a good idea to put the flush and close in a finally, so that no matter what happens it gets closed. Plus I add a if not null so it looks like this...

finally
{
if (bw != null)
{
bw.flush();
bw.close();
}
}

I do this everywhere.

Nicole
 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made the changes as told by you but it isn't making much diffrence

If some other application is reading and writting from files like Virus scan(Which is reading files from scanning ) and windows process lsass.exe which is reading and writing files for security authorizations.

Can these applications cause the problem??
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hemant Khurana:

Can these applications cause the problem??


Unlikely, as other applications would have seperate descriptor tables.
Try using Process Explorer to examine what file descriptors your appliation has open. You may have the same problem with other files/sockets/database connections/etc.
 
Nicole Lacoste
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try putting a bw.flush() just after the bw.newLine(). Maybe the file output buffer is overloading?

Niki
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BufferedWriter (and all buffered classes) checks to see when its buffer is going to overflow and flushes automatically. You can check for yourself. The source for the JDK classes is in a src.zip file in your JDK install directory (if you opted to install it, that is).
In any case, I'd expect a problem in the Writer to throw an IOException. An exception reporting "too many open files" is almost certainly related to the OS's restriction on the number of file descriptors a process may have open at one time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic