S
S
SlavaMorg2017-09-12 17:46:42
Java
SlavaMorg, 2017-09-12 17:46:42

How to close unreferenced streams in Java?

For example, I need to read only one line. I do this (of course, maybe it's not possible to do this):

String s = (new BufferedReader(new InputStreamReader(System.in))).readLine();

How can I close these streams? Will they close themselves or will they still take up resources? Is that how they do it or is it bad manners?
Or even if you write like this (that's exactly what they do):
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.close();

Also, I can't explicitly close the InputStreamReader. And how to be?
Forgive, if that, for elementary questions. Couldn't find it on google, maybe I spelled it wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2017-09-12
@SlavaMorg

Everything is pretty simple.
Those. BufferedReader.close() will also free all resources by itself.
To understand exactly, look here the source on line 520.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question