• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i close files immediately after using them
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
system.exit()
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic