Java import java. If close method of input stream will throw an Exception then output stream will not be closed, i. This means the file descriptor held by OutputStream will never release causing a resource leak in the Java program. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. If you are considering using a finalizer to ensure that important resources are freed in a timely manner, you might want to reconsider.
If you can import Apache Commons IO , you could make use of the ever-so-handy AutoCloseInputStream s classes: you wrap your InputStream and then you just use your wrapped instance and it gets automatically closed as soon as the end of input has been reached or when the stream is explicitly closed, whichever comes first. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. Asked 11 years ago. Active 1 year, 9 months ago. Viewed 98k times. Or does it really matter? Improve this question. Jason Watkins Jason Watkins 1, 4 4 gold badges 17 17 silver badges 30 30 bronze badges.
Thanks to everyone for the answers you provided. I wish I could accept them all as THE answer. This now makes sense to me. Add a comment. Active Oldest Votes. Improve this answer. Jon Jon Do Nhu Vy Daniel Voina Daniel Voina 2, 27 27 silver badges 30 30 bronze badges. Bill the Lizard Bill the Lizard k gold badges silver badges bronze badges. Thanks for the link! Rory McCrossan k 37 37 gold badges silver badges bronze badges. Adrian Smith Adrian Smith It's important to close streams, to release file descriptor held by this class, as its limited resource and used in both socket connection and file handling.
A serious resource leak may result in file descriptor exception as well. Before moving ahead, let's see the part of the code candidate wrote for copying file from one directory to another directory in Java without using any third-party library.
Share to Twitter Share to Facebook. Labels: best practices , core java , java IO tutorial. October 12, at AM Anonymous said November 14, at AM Anonymous said December 10, at PM Amit said April 12, at AM Anonymous said Thank you! July 27, at AM. Newer Post Older Post Home. Subscribe to: Post Comments Atom. Subscribe for Discounts and Updates Follow. Search This Blog.
0コメント