Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i close files immediately after using them
 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
system.exit()
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nirmal Mekala Kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aruneesh Salhotra
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic