欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中使用Closeable接口自動(dòng)關(guān)閉資源詳解

 更新時(shí)間:2023年12月02日 08:34:47   作者:nuomizhende45  
這篇文章主要介紹了Java中使用Closeable接口自動(dòng)關(guān)閉資源詳解,Closeable接口繼承于AutoCloseable,主要的作用就是自動(dòng)的關(guān)閉資源,其中close()方法是關(guān)閉流并且釋放與其相關(guān)的任何方法,如果流已被關(guān)閉,那么調(diào)用此方法沒有效果,需要的朋友可以參考下

Closeable接口自動(dòng)關(guān)閉資源

Closeable接口繼承于AutoCloseable,主要的作用就是自動(dòng)的關(guān)閉資源,其中close()方法是關(guān)閉流并且釋放與其相關(guān)的任何方法,如果流已被關(guān)閉,那么調(diào)用此方法沒有效果,像 InputStream和OutputStream類都實(shí)現(xiàn)了該接口,源碼如下

/*
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.io;
import java.io.IOException;
/**
 * A {@code Closeable} is a source or destination of data that can be closed.
 * The close method is invoked to release resources that the object is
 * holding (such as open files).
 *
 * @since 1.5
 */
public interface Closeable extends AutoCloseable {
    /**
     * Closes this stream and releases any system resources associated
     * with it. If the stream is already closed then invoking this
     * method has no effect.
     *
     * <p> As noted in {@link AutoCloseable#close()}, cases where the
     * close may fail require careful attention. It is strongly advised
     * to relinquish the underlying resources and to internally
     * <em>mark</em> the {@code Closeable} as closed, prior to throwing
     * the {@code IOException}.
     *
     * @throws IOException if an I/O error occurs
     */
    public void close() throws IOException;
}

Closeable用法

1.在1.7之前,我們通過try{} finally{} 在finally中釋放資源。

2.對(duì)于實(shí)現(xiàn)AutoCloseable接口的類的實(shí)例,將其放到try后面(我們稱之為:帶資源的try語(yǔ)句),在try結(jié)束的時(shí)候,會(huì)自動(dòng)將這些資源關(guān)閉(調(diào)用close方法)。

test1方法是最常規(guī)的try{}catch{}finally{}

test2是使用closeable自動(dòng)釋放資源

package com.canal.demo;
import java.io.Closeable;
import java.io.IOException;
public class CloseableTest implements Closeable {
    public static void test1() {
        try {
            System.out.println("test1方法 處理邏輯");
        } catch (Exception e) {
            System.out.println("test1方法 異常處理");
        } finally {
            System.out.println("test1方法 釋放資源");
        }
    }
    public static void test2() {
        try (CloseableTest c = new CloseableTest()) {
            System.out.println("test2方法 處理邏輯");
        } catch (Exception e) {
            System.out.println("test2方法 處理異常");
        }
    }
    @Override
    public void close() throws IOException {
        System.out.println("test2方法這里自動(dòng)釋放資源");
    }
    public static void main(String[] args) {
        test1();
        test2();
    }
}

運(yùn)行結(jié)果如下

到此這篇關(guān)于Java中使用Closeable接口自動(dòng)關(guān)閉資源詳解的文章就介紹到這了,更多相關(guān)Closeable接口自動(dòng)關(guān)閉資源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java的函數(shù)方法詳解(含漢諾塔問題)

    Java的函數(shù)方法詳解(含漢諾塔問題)

    漢諾塔問題是一個(gè)經(jīng)典的遞歸問題,下面這篇文章主要給大家介紹了關(guān)于Java函數(shù)方法(含漢諾塔問題)的相關(guān)資料,文中通過圖文以及代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • Java EasyExcel讀寫excel如何解決poi讀取大文件內(nèi)存溢出問題

    Java EasyExcel讀寫excel如何解決poi讀取大文件內(nèi)存溢出問題

    這篇文章主要介紹了Java EasyExcel讀寫excel如何解決poi讀取大文件內(nèi)存溢出問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • spring 整合mybatis后用不上session緩存的原因分析

    spring 整合mybatis后用不上session緩存的原因分析

    因?yàn)橐恢庇胹pring整合了mybatis,所以很少用到mybatis的session緩存。什么原因呢?下面小編給大家介紹spring 整合mybatis后用不上session緩存的原因分析,需要的朋友可以參考下
    2017-02-02
  • Spring的BeanFactoryPostProcessor接口示例代碼詳解

    Spring的BeanFactoryPostProcessor接口示例代碼詳解

    這篇文章主要介紹了Spring的BeanFactoryPostProcessor接口,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Java實(shí)現(xiàn)快速排序過程分析

    Java實(shí)現(xiàn)快速排序過程分析

    今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)快速排序過程分析,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件

    Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件

    這篇文章主要介紹了Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件,下面文章圍繞FileUtils的相關(guān)資料展開怎么讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件的內(nèi)容,具有一定的參考價(jià)值,徐婭奧德小伙伴可以參考一下
    2021-12-12
  • Java的無參構(gòu)造函數(shù)用法實(shí)例分析

    Java的無參構(gòu)造函數(shù)用法實(shí)例分析

    這篇文章主要介紹了Java的無參構(gòu)造函數(shù)用法,結(jié)合實(shí)例形式分析了java無參構(gòu)造函數(shù)基本原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • IDEA 包轉(zhuǎn)模塊的解決步驟

    IDEA 包轉(zhuǎn)模塊的解決步驟

    很多朋友遇到這樣一個(gè)問題,直接在idea拉取代碼,發(fā)現(xiàn)創(chuàng)建的模塊包類型不一樣了,類似這樣的問題該如何處理呢?很多朋友向小編求助,在這統(tǒng)一回答大家,需要的朋友參考下本文吧
    2021-06-06
  • Spring Boot與Docker部署實(shí)踐

    Spring Boot與Docker部署實(shí)踐

    這篇文章主要介紹了Spring Boot與Docker部署實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • 深入解析反編譯字節(jié)碼文件中的代碼邏輯JVM中的String操作

    深入解析反編譯字節(jié)碼文件中的代碼邏輯JVM中的String操作

    這篇文章主要介紹了深入解析反編譯字節(jié)碼文件中的代碼邏輯JVM中的String操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10

最新評(píng)論