Solved

closing a FileReader / BufferReader

Posted on 2002-05-26
2
418 Views
Last Modified: 2010-08-05
Hi,

1. When using FileReader or BufferReader, do you must close the object?
2. What happens if the file remains open and there's no close call?

Thanks Kayzer.
0
Comment
Question by:kayzer
  • 2
2 Comments
 
LVL 7

Accepted Solution

by:
tomboshell earned 130 total points
Comment Utility
As I see it, it is good programming practice.  The garbage collector takes care of variables when they are no longer used.  This is usually after the exit from a block of code (ie a function block or a sub-class, ect).  Then the gc may release the block of memory for the local and temp variables automatically.  However, if you readers are declared globally  then they will exist the entire time the program is running-thereby using up a lot of resources.  Leaving file pointers (readers or writers) running free you may incure read/write blocks.  If that happens you may wonder why some other code can't work with that file.  Also, what if you program grows extremely large?  Will you be able to find the proper spot to close the file again, thereby optimizing that area of code; at the same time, would others maintaining your code be capable of finding the open reader?  It is just better to close the readers.  
If you leave it open, at some time the program must be exited.  Then it will be closed by exiting the virtual machine.  If some other error or memory problem doesn't occur before hand.  
0
 
LVL 7

Expert Comment

by:tomboshell
Comment Utility
Thanks for the grade!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now