Class TryWithResourcesExamples
java.lang.Object
net.jrodolfo.java_evolution.java09.TryWithResourcesExamples
Demonstrates the Java 9 try-with-resources improvement.
Java 7 introduced try-with-resources, but a resource usually had to be
declared inside the try (...) block. When a resource was already
stored in a final or effectively final variable, code often repeated the
variable just to satisfy the syntax.
Java 9 made it possible to use an existing final or effectively final resource variable directly in the resource list.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionreadFirstLine(BufferedReader reader) Reads the first line and closes the provided reader using the Java 9 try-with-resources style.
-
Constructor Details
-
TryWithResourcesExamples
public TryWithResourcesExamples()
-
-
Method Details
-
readFirstLine
Reads the first line and closes the provided reader using the Java 9 try-with-resources style.- Parameters:
reader- an effectively final resource created before the try block- Returns:
- the first line from the reader
- Throws:
IOException- when reading fails
-