The "Java doubts" input and output stream is used to close the problem in time

Source: Internet
Author: User


The code is as follows:

public class Example041 {public static void main (string[] args) throws IOException {Example041 e41 = new Example041 (); e41. Copy ("d:\\ name Ape_it.txt", "d:\\", "Love the Ape. txt");} private void Copy (String src, String dest) throws IOException {InputStream is = null;outputstream OS = null;try {is = new FileInputStream (src); os = new FileOutputStream (dest); byte[] buf = new Byte[1024];int N;while ((n = is.read (buf)) > 0) { Os.write (buf, 0, N);}} finally {is.close (); Os.close ();}}}


Code Analysis:

There is no problem with normal execution of the above code, but there are hidden bugs. The bug is in the finally statement block. First, the IS and OS do not make a null judgment when shutting down, and secondly, if an error throws an exception when the Is.close is closed, then Os.close will not execute, resulting in the file output stream not being closed. A change to the finally statement is as follows:

if (is = null) {try {is.close ();    } catch (IOException e) {//Do nothing}}if (OS! = null) {try {os.close (); } catch (IOException e) {//Do nothing}}

The code above is repeated two times in writing, so you can refactor as follows:

void Copy2 (String src, String dest) throws IOException {InputStream is = null;outputstream OS = null;try {is = new FILEINP Utstream (src); os = new FileOutputStream (dest); byte[] buf = new Byte[1024];int N;while ((n = is.read (buf)) > 0) {os.writ E (buf, 0, N);}} Finally {close (IS); close (OS);}} private void Close (closeable closeable) {if (closeable! = null) {try {closeable.close ();} catch (IOException e) {//Do not Hing}}}


In Summary, when you call the Close method in a finally statement block, you protect it with a nested Try-catch statement to prevent IOException from propagating. More generally, any checked exceptions that might be thrown in a finally statement block are handled rather than propagated.


note: This "Java Confusion" series are bloggers reading "Java doubts" The original book after the interpretation of the original book and examples of the adaptation and then write a post to publish. All examples are personally tested and shared on GitHub. Use these examples to motivate yourself to benefit others. At the same time, all the posts in this series will be published in the blogger personal public search "Love Ape" or "ape_it" to facilitate reading. If there is any infringement of the original rights of the author, please inform the blogger in time to delete if the reader has objections to the content of the text or questions are welcome through the blog post or public messages and other ways to discuss together.

Source code Address Https://github.com/rocwinger/java-disabuse


This article from "Winger" blog, declined reprint!

The "Java doubts" input and output stream is used to close the problem in time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.