How do I close a file after accessing it?

Tim Rohaly

Creating a new instance of File doesn't open the file - that's why File doesn't have a close method. The only way you can access the contents of a file is using RandomAccessFile, FileInputStream, FileOutputStream. FileReader, or FileWriter. Each of these classes does have an explicit close() method.

Note that you should always make it a practice to close your file streams after you have finished using them. It is not enough to set the stream reference to null because you are likely to run out of available file descriptors long before you run out of VM memory - and garbage collection is only triggered by memory shortage, not shortage of descriptors.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact