Correct Ways to Close InputStream and OutputStream in Java with Examples
Since IO needs you to deal with streams, channels, and file descriptors that need to be closed properly, it is uncomfortable for Java developers to deal with them. Because of this, many Java programmers never bother doing clean-up, the laziness of just doing work and ignoring anything is really easy. For programmers who have never done system programming using C or C++, this habit is more visible.
To release the file descriptor held by this class as its limited resource and used in both socket connection and file handling, it is essential to close streams. A severe resource leak can also lead to an exception in the file descriptor.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Let’s see a code for copying a file from one directory to another directory in Java without using any third-party library.
Most of the code is al-right and even better than many Java programmers. but it has an error that could cause a resource leak in the Java program. If close() method of input stream will throw an Exception then output stream will not be closed, i.e. fos.close() will not even execute if fis.close() throws exception. This means the file descriptor held by OutputStream will never release causing a resource leak in the Java program. It’s not uncommon.
Here is the right way of closing InputStream and OutputStream in Java :
If is.close() throws an IOException, this code would not fail to call os.close(), which would guarantee that the file descriptor kept by OutputStream is released.