溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java面試try-with-resources問題怎么解答

發(fā)布時(shí)間:2022-07-19 09:26:58 來源:億速云 閱讀:126 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“java面試try-with-resources問題怎么解答”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

 前言:

這個(gè)語(yǔ)句的作用是,確保該語(yǔ)句執(zhí)行之后,關(guān)閉每一個(gè)資源,也就是說它確保了每個(gè)資源都在生命周期結(jié)束之后被關(guān)閉,因此,比如讀寫文件,我們就不需要顯示的調(diào)用close()方法

這個(gè)語(yǔ)句的大致模板如下:

java面試try-with-resources問題怎么解答

我們可以看到我們把需要關(guān)閉的資源都放到try()這個(gè)括號(hào)里面去了,之前都是對(duì)異常的捕獲,怎么還可以寫資源語(yǔ)句,這就是奇妙之處,注意分號(hào)啊,最后一個(gè)資源可以不用加分號(hào),中間的都要加分號(hào),并且,對(duì)于java7來說,變量的聲明必須放在括號(hào)里面。 

下面來說一下具體實(shí)現(xiàn)原理:

首先在try()里面的類,必須實(shí)現(xiàn)了如下這個(gè)接口

java面試try-with-resources問題怎么解答

這個(gè)接口也叫自動(dòng)關(guān)閉資源接口.我們想要寫這樣的語(yǔ)句,必須去實(shí)現(xiàn)它里面的close()方法

java面試try-with-resources問題怎么解答

對(duì)于這個(gè)類,很多類都已經(jīng)做了默認(rèn)實(shí)現(xiàn),所以我們沒有必要顯示去山實(shí)現(xiàn)這樣一個(gè)東西,直接拿來用就可以了,比如:

java面試try-with-resources問題怎么解答

 我們這些常見的文件操作類,都已經(jīng)做了實(shí)現(xiàn)。

這樣的做法有助于我們寫非常復(fù)雜的finally塊,話不多說,直接上代碼:

ImageCopy.java

import java.io.*;
public class ImageCopy {
    public static void main(String[] args) {
        File srcFile = new File("F:\\java課程資料\\王也.png");
        File destFile = new File("E:\\717.png");
        copyImage(srcFile,destFile);
    }
    //這個(gè)采用一邊讀,一邊寫的思路來做
    public static void copyImage(File srcFile, File destFile)  {
        //這個(gè)太繁瑣了,我們把它進(jìn)行改進(jìn)
       /* FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            byte[] buff = new byte[1024];
            int len = 0;
            while((len = fis.read(buff)) != -1) {
                fos.write(buff,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }*/
       //這里會(huì)自動(dòng)幫我們關(guān)閉打開的這些資源
       try( FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos)
       ) {
           byte[] buff = new byte[1024];
           int len = 0;
           while((len = bis.read(buff)) != -1) {
               bos.write(buff,0,len);
           }
       }catch (Exception e) {
            e.printStackTrace();
        }
    }
    //采用字符流來讀取文本操作
    public static void copyText(File srcFile,File destFile) {
        InputStreamReader fr = null;
        OutputStreamWriter fw = null;
        try {
            fr = new InputStreamReader(new FileInputStream(srcFile),"gbk");
          //  fw = new FileWriter(destFile);
            fw = new OutputStreamWriter(new FileOutputStream(destFile),"gbk");
            char[] buff = new char[1024];
            int len = 0;
            while((len = fr.read(buff)) != -1) {
                System.out.println("讀取到的長(zhǎng)度:" + len);
                fw.write(buff,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

“java面試try-with-resources問題怎么解答”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI