aspose file tools*
The moose likes Java in General and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception in thread "main" java.io.IOException: Too many open files" Watch "Exception in thread "main" java.io.IOException: Too many open files" New topic
Author

Exception in thread "main" java.io.IOException: Too many open files

Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
How do i close files immediately after using them


Nirmal Kumar<br />If you have to aim, aim for the moon. Even if you miss you will land among the stars
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
system.exit()
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Originally posted by Atrus Greyor:
system.exit()

Cute

Clear (set to null) any references to them then call System.gc(), this will usually do the trick
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Every Reader, Writer, InputStream, and OutputStream has a close() method. Be sure to call it when you're through with the file you're reading or writing.


[Jess in Action][AskingGoodQuestions]
Nirmal Mekala Kumar
Ranch Hand

Joined: Aug 27, 2005
Posts: 63
Thank you guys but cant use System.exit() because my program has to be still running when i close the files. Also i'm not using any I/O streams. I'm just initializing the files with a path to see if they exist. So i cant use the stream close methods.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

I'm just initializing the files with a path to see if they exist.

What exactly do you mean by that? If you're only creating java.io.File objects and you use the method exists() to check if the file or directory exists, you are not opening any files on the file system, and this is not the cause of your problem.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167
If you are writing to a file, before you call the file.close(), you should always call, file.flush(), so that any thing in the buffers get written to the file, in the first place.

By the way, For readers, there is no flush().
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Aruneesh Salhotra:
If you are writing to a file, before you call the file.close(), you should always call, file.flush(), so that any thing in the buffers get written to the file, in the first place.


No. As long as you're closing the same Writer you've been writing to (good practice in general!) this will always happen automatically. BufferedWriter, etc, will call flush() and then close() on their target when you call close() on them.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Nirmal Mekala Kumar:
Thank you guys but cant use System.exit() because my program has to be still running when i close the files. Also i'm not using any I/O streams. I'm just initializing the files with a path to see if they exist. So i cant use the stream close methods.


It sounds like you need to post some code in order to illustrate what you are trying to do. At this point, we can only guess at how to fix the problem, and as you can see, we seem to be guessing incorrectly. The exact code that causes the problem will help clarify what is going on.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.io.IOException: Too many open files