Java 7 – try-with-resources Statement
Java
207
java7
1
try-with-resources
2
Male avatar

loveprogramming viết ngày 19/04/2021

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

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

451 bài viết.
79 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
451 bài viết.
79 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!