Solved

closing a FileReader / BufferReader

Posted on 2002-05-26
2
430 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
ID: 7035202
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
ID: 7035458
Thanks for the grade!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb on wildfly 5 30
eclipse console opening separately 2 29
ejb message driven bean mdb creation steps 2 15
Java Eclipse Loop 3 20
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

789 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