Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How close Java Input Streams?

Tags:

java

io

stream

In the following code:

DataInputStream in = new DataInputStream(
          new BufferedInputStream(new FileInputStream(file)));
in.close();

Do I need to close the 2 other stream in addition to closing the "top level" stream?

like image 269
Manuel Selva Avatar asked Oct 20 '10 08:10

Manuel Selva


2 Answers

if you look the source of DataInputStream you can see that it closes the underlying streams as well. so you don't need. and this is (or should be) true for all type of streams.

like image 60
KARASZI István Avatar answered Sep 23 '22 07:09

KARASZI István


I will use this opportunity to answer with an answer I have already made before.

By using Project Lombok you can let Lombok correctly close the streams for you. Details can be found here.

like image 27
Shervin Asgari Avatar answered Sep 25 '22 07:09

Shervin Asgari