Java 7 - try-with-resources Statement » grokonez

Java 7 - try-with-resources Statement » grokonez

Java 7 provides a new approach for closing resources with clean & clear code by try-with-resources statment. In the article, JavaSampleApproach will introduce the benifit when programming by try-with-resources statement. Related Post: Understand Java Exception & The effect to Java Program (Single & Multi Thread) ContentsI. ConceptsII. Practice1. Use Java6 or early version and Problems2. … Continue reading "Java 7 – try-with-resources Statement"

https://grokonez.com/java/java-advanced/java-7-try-with-resources-statement

Java 7 – try-with-resources Statement

Java 7 provides a new approach for closing resources with clean & clear code by try-with-resources statment. In the article, JavaSampleApproach will introduce the benifit when programming by try-with-resources statement.

Related Post: Understand Java Exception & The effect to Java Program (Single & Multi Thread)

I. Concepts

What is a resource? An object that implements java.lang.AutoCloseable or java.io.Closeable is called a resource. Some Java resource classes: - java.io.BufferedReader.BufferedReader - java.net.Socket.Socket - java.sql.Statement ... try-with-resources statement ensures that each resource is closed after completed execution of the statement.

II. Practice

For see benifits of try-with-resources statement, JavaSampleApproach makes an sample with BufferedReader for reading a text file.

1. Use Java6 or early version and Problems


package com.javasampleapproach.trywithresources;

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;

public class ReadFileByJava6OrEarly { public static void main(String[] args) { BufferedReader br = null; try { br = new BufferedReader(new FileReader("C://readfile/input.txt")); String line; while (null != (line = br.readLine())) { // process each line of File System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != br) br.close(); } catch (IOException e) { e.printStackTrace(); } } } }

More at:

https://grokonez.com/java/java-advanced/java-7-try-with-resources-statement

Java 7 – try-with-resources Statement

java java7 try-with-resources

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Going Beyond Java 8: Local Variable Type Inference (var) - DZone Java

In this article, we will talk about the most important new feature introduced with Java 10, officially called local variable type inference. An extremely important function in java. You will regret skipping this article.

New to Java? Here Are Some Resources

If you're looking to learn how to code in Java, you should be taking advantage of these resources. Whatever your preferred style of learning, there is something here for you. A huge vault of resources that is completely free. Don't miss it.

Java 9 try-with-resources Improvement » grokonez

Java 9 Try-with-resources makes an improved way of writing code. Now we can simplify our code and keep it cleaner and clearer.

Java Online Training | Java Online Course | ITGuru

Our Java Online Training provide you to learn about Java programming and its different features with realty. Our Java Online Course includes live sessions, live projects