InputStream resources

K

Kevin Kirkeby

I have usually gotten properties from a file as follows:
properties.load(new FileInputStream(propFile));

After I'm finished I just return from the method that invoked the above
statement. I've been assuming that after I exit my method, the garbage
collector *should* clean things up; however, am I incorrect in this
assumption? i.e. are resources not being released?

Is this a bad programming practice or should I do more of the following:
FileInputStream fis = new FileInputStream(propFile);
properties.load(fis);
fis.close();

Thanks!
 
A

Adam Maass

Kevin Kirkeby said:
I have usually gotten properties from a file as follows:
properties.load(new FileInputStream(propFile));

After I'm finished I just return from the method that invoked the above
statement. I've been assuming that after I exit my method, the garbage
collector *should* clean things up; however, am I incorrect in this
assumption? i.e. are resources not being released?

Is this a bad programming practice or should I do more of the following:
FileInputStream fis = new FileInputStream(propFile);
properties.load(fis);
fis.close();

You should explicitly close the FileInputStream.

Garbage collection will close it for you -- if it ever happens. And there's
no gurantee that it will before something bad happens.

-- Adam Maass
 
R

Roedy Green

Is this a bad programming practice or should I do more of the following:
FileInputStream fis = new FileInputStream(propFile);
properties.load(fis);
fis.close();

look in SRC.ZIP. I think the odds are you will see Properties.load
just that. You might as well use the short code. So long as you
don't gratuitously store references to things, they will be garbage
collected naturally.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top