D
D
Dik_Nik2019-06-20 20:34:31
Java
Dik_Nik, 2019-06-20 20:34:31

How to properly close nested streams in Java?

I have this code:

try (final FSDataInputStream fileStream = getFileInputStream() {
     Table table = csvUtils.getData(tableInfo, new BufferedReader(new InputStreamReader(fileStream)));
}

Can you please tell me if this code will leak if I close the InputStream but not the BufferedReader?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-06-20
@Dik_Nik

Why not do so?

try (BufferedReader reader = new BufferedReader(new InputStreamReader(getFileInputStream()))) {
    Table table = csvUtils.getData(tableInfo, reader);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question