aspose file tools*
The moose likes I/O and Streams and the fly likes java.io.FileNotFoundException  ( Too many open files ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java.io.FileNotFoundException  ( Too many open files )" Watch "java.io.FileNotFoundException  ( Too many open files )" New topic
Author

java.io.FileNotFoundException ( Too many open files )

Hemant Khurana
Greenhorn

Joined: Jan 11, 2007
Posts: 27
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

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Hemant Khurana
Greenhorn

Joined: Jan 11, 2007
Posts: 27
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

Joined: Aug 16, 2005
Posts: 14435
    
  23

Opening and not closing a file, even if it's only for reading, can certainly cause this problem.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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

Joined: Jan 11, 2007
Posts: 27
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

Joined: Nov 29, 2005
Posts: 2308
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

Joined: Oct 04, 2006
Posts: 30
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

Joined: Jan 11, 2007
Posts: 27
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

Joined: Oct 29, 2001
Posts: 8997
    
    9

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.


[How To Ask Questions On JavaRanch]
Nicole Lacoste
Ranch Hand

Joined: Oct 04, 2006
Posts: 30
Hi,

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

Niki
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io.FileNotFoundException ( Too many open files )