Original link Jakob Jenkov Translator: Li Jing ([email protected])
Flow with reader and writer at the end of use, you need to close them correctly. This can be achieved by calling the close () method. But it takes some thinking. Take a look at the code below:
01 |
InputStream input = newFileInputStream("c:\\data\\input-text.txt"); |
03 |
intdata = input.read(); |
07 |
//do something with data... |
09 |
doSomethingWithData(data); |
At first glance at this piece of code, you may feel that there is nothing wrong. But what happens if an exception occurs when the Dosomethingwithdata () method is called? Yes, this InputStream object will not be closed.
To avoid an exception, the stream cannot be closed, and we can rewrite the code like this:
01 |
InputStream input = null; |
05 |
input = newFileInputStream("c:\\data\\input-text.txt"); |
07 |
intdata = input.read(); |
11 |
//do something with data... |
13 |
doSomethingWithData(data); |
19 |
}catch(IOException e){ |
21 |
//do something with e... log, perhaps rethrow etc. |
Notice here that the InputStream's closing code is placed in the finally block, and the code in the finally will always be executed regardless of what happens in the Try-catch block, so this inputstream is always closed.
But what if the close () method throws an exception and tells you that the stream has been closed? To solve this problem, you also need to write the close () method inside the Try-catch, like this:
09 |
} catch(IOException e){ |
11 |
//do something, or ignore. |
This section solves the problem of InputStream (or OutputStream) stream shutdown, and indeed there are some things that are not graceful, although the exceptions can be handled correctly. This is not a good solution if your code is repeated throughout this ugly exception-handling code. What if a hurried fellow was greedy for convenience and ignored exception handling?
Also, imagine that an exception was first thrown from within the Dosomethingwithdata method. The first catch catches the exception, and then in the Finally, the program tries to close the InputStream. But what if there are exceptions thrown from within the close () method? Which of these two exceptions should be propagated on the call stack?
Fortunately, there is a way to solve this problem. This solution is referred to as the exception handling template. Create a template that correctly closes the stream, which can be written and reused in code, both elegant and simple. See Java exception Handling templates for details.
Exception handling of IO in Java7
Starting with Java7, a new exception-handling mechanism called "Try-with-resource" is introduced. This mechanism is designed to address exception handling for resources such as InputStream and outputstream that need to be closed after use. You can browse try with Resource in Java 7 for more information.
original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java IO: Exception handling
Java IO: Exception handling