Explain try-with-resources statement introduced in Java 7.

Answered

Explain try-with-resources statement introduced in Java 7.

Ninja Asked on 18th September 2018 in Java.
Add Comment
1 Answer(s)
Best answer

The try-with-resources statement introduced in Java SE 7 is particularly suited to situations that use Closeable resources, such as streams. The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

The following example reads the first line from a file using an instance of BufferedReader.

static String readFirstLineFromFile(String path) throws IOException {
 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  return br.readLine();
 }
}

In this example, the resource declared in the try-with-resources statement is a BufferedReader. The declaration statement appears within parentheses immediately after the try keyword. The class BufferedReader, in Java SE 7 and later, implements the interface java.lang.AutoCloseable. Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).

Ninja Answered on 18th September 2018.
Add Comment