Two Ways to Close a Stream - java.io - | Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Chapter: Java The Complete Reference - The Java Library - Input/Output: Exploring java.io

Two Ways to Close a Stream - java.io

In general, a stream must be closed when it is no longer needed. Failure to do so can lead to memory leaks and resource starvation.

Two Ways to Close a Stream

 

In general, a stream must be closed when it is no longer needed. Failure to do so can lead to memory leaks and resource starvation. The techniques used to close a stream were described in Chapter 13, but because of their importance, they warrant a brief review here before the stream classes are examined.

Beginning with JDK 7, there are two basic ways in which you can close a stream. The first is to explicitly call close( ) on the stream. This is the traditional approach that has been used since the original release of Java. With this approach, close( ) is typically called within a finally block. Thus, a simplified skeleton for the traditional approach is shown here:

 

try {

 

         open and access file } catch( I/O-exception) {

         ...

 

} finally {

 

         close the file

 

}

 

This general technique (or variation thereof) is common in code that predates JDK 7. The second approach to closing a stream is to automate the process by using the

 

try-with-resources statement that was added by JDK 7 (and, of course, supported by JDK 8). The try-with-resources statement is an enhanced form of try that has the following form:

 

try (resource-specification) { // use the resource

}

 

 

Here, resource-specification is a statement or statements that declares and initializes a resource, such as a file or other stream-related resource. It consists of a variable declaration in which the variable is initialized with a reference to the object being managed. When the try block ends, the resource is automatically released. In the case of a file, this means that the file is automatically closed. Thus, there is no need to call close( ) explicitly.

Here are three key points about the try-with-resources statement:

 

      Resources managed by try-with-resources must be objects of classes that implement

 

AutoCloseable.

 

      The resource declared in the try is implicitly final.

 

      You can manage more than one resource by separating each declaration by a semicolon.

 

Also, remember that the scope of the declared resource is limited to the try-with-resources statement.

The principal advantage of try-with-resources is that the resource (in this case, a stream) is closed automatically when the try block ends. Thus, it is not possible to forget to close the stream, for example. The try-with-resources approach also typically results in shorter, clearer, easier-to-maintain source code.

Because of its advantages, try-with-resources is expected to be used extensively in new code. As a result, most of the code in this chapter (and in this book) will use it. However, because a large amount of older code still exists, it is important for all programmers to also be familiar with the traditional approach to closing a stream. For example, you will quite likely have to work on legacy code that uses the traditional approach or in an environment that uses an older version of Java. There may also be times when the automated approach is not appropriate because of other aspects of your code. For this reason, a few I/O examples in this book will demonstrate the traditional approach so you can see it in action.

One last point: The examples that use try-with-resources must be compiled by a modern version of Java. They won’t work with an older compiler. The examples that use the traditional approach can be compiled by older versions of Java.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2021 BrainKart.com; All Rights Reserved. (BS) Developed by Therithal info, Chennai.