Search code examples
javabufferedreader

Closing a BufferedReader


If I invoke a BufferedReader the following way:

Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());

Will the JVM know to automatically close it when not in use? If not, how do I go about closing it?


Solution

  • If you are using java 7 or greater and your code is in try catch resource block, then it is Auto closes.

    If in below versions you have to close with close(). For that you have to change your current way of using and get the reference.