[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item09 ์™„๋ฒฝ๊ณต๋žต.

2023. 1. 25. 15:25ใ†JAVA/Effective JAVA

728x90

 

item09. try-finally๋ณด๋‹ค try-with-resources ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

" p48. ์ž๋ฐ” ํผ์ฆ๋Ÿฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ์˜ ์‹ค์ˆ˜"

package chapter01.item09.puzzler;

import java.io.*;

/**
 * item09. try-finally ๋ณด๋‹ค๋Š” try-with-resources๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.
 * ์ž๋ฐ” Puzzler ์ฑ…์„ ์–ธ๊ธ‰ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ์˜ ์‹ค์ˆ˜
 */
public class Copy {

    private static final int BUFFER_SIZE = 8 * 1024;

    static void copy(String src, String dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);
        try {
            byte[] buf = new byte[BUFFER_SIZE];
            int n;
            while ((n = in.read(buf)) >= 0) {
                out.write(buf, 0, n);
            }
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                // ์•ˆ์ „ํ•˜์ง€ ์•Š๋Š”๊ฐ€? ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€?
                // No, ๋‹ค๋ฅธ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋๋‚˜๋ฒ„๋ฆผ,,
            }

            try {
                in.close();
            } catch (IOException e) {
                // ์•ˆ์ „ํ•˜์ง€ ์•Š๋Š”๊ฐ€? ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€?
            }
        }
    }

    public static void main(String[] args) throws IOException {
        String src = args[0];
        String dst = args[1];
        copy(src, dst);
    }
}

 

์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” ์ฑ…์—์„œ "์‹ฌ์ง€์–ด ๋‚˜ ์ž์‹ ๋„ '์ž๋ฐ” ํผ์ฆ๋Ÿฌ' ์—์„œ ์‹ค์ˆ˜๋ฅผ ์ €์งˆ๋ €ใ„ดใ„ด๋ฐ, ์ˆ˜๋…„๊ฐ„ ์•„๋ฌด๋„ ๋ˆˆ์น˜์ฑ„์ง€ ๋ชปํ–ˆ๋‹ค" ๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ถ€๋ถ„์ด ์žˆ๋‹ค. 

๋‚ด์šฉ์€ ๋Œ€์ถฉ ์œ„์— ์ฝ”๋“œ์™€ ๋น„์Šทํ•œ ๋‚ด์šฉ์ด๋ผ๊ณ  ํ•œ๋‹ค. 

out.close ์™€ in.close์—์„œ ๊ฐ๊ฐ try-catch๋กœ ์žก์•„์ฃผ๋ฉด ์•ˆ์ „ํ•  ๊ฒƒ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋  ๊ฒƒ์ด๋‹ค. ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ 

์—ฌ๊ธฐ์„œ ๋ณด๋ฉด IOException์ด ๋ฐœ์ƒํ•  ๋•Œ๋Š” ๊ดœ์ฐฎ์ง€๋งŒ out.close ํ•  ๋•Œ RuntiimeException์ด ๋ฐœ์ƒํ•ด๋ฒ„๋ฆฌ๋ฉด ์ €๊ธฐ์„œ ๋๋‚˜๋ฒ„๋ ค In.close๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ๋ชปํ•œ๋‹ค. 

 

์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์ฃผ๋Š” try-with-resources ๊ฐ€ ์žˆ์œผ๋‹ˆ ์ด์   try-finally๊ฐ€ ์•„๋‹Œ try-with-resources๋ฅผ ์‚ฌ์šฉํ•˜์ž!! 

 

 

 

 

" p49. try-with-resources ๋ฐ”์ดํŠธ ์ฝ”๋“œ"

try-with-resources๋Š” ์–ด๋–ป๊ฒŒ ์˜ˆ์™ธ ์ •๋ณด๋„ ๋ชจ๋‘ ๋ณด์—ฌ์ฃผ๊ณ , ์ž์› ํ•ด์ œ๋„ ๋‹ค ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ๋œ๋‹ค. 

๊ทผ๋ฐ ๋ฐ”์ดํŠธ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ๋ณด๋ฉด์„œ ํŒŒ์•…ํ•˜๊ธฐ์—๋Š” ํž˜๋“œ๋‹ˆ๊นŒ..ใ…Žใ…Ž IDE์˜ ํž˜์„ ๋นŒ๋ ค์„œ classes ํด๋” ์•ˆ์— ์žˆ๋Š” ํŒŒ์ผ์„ ๋ณด๋ฉด ๋œ๋‹ค. 

 

public class TopLine {
    // try-with-resources ์ด ์ž์›์„ ํšŒ์ˆ˜ํ•˜๋Š” ์ตœ์„ ์ฑ…์ด๋‹ค.
    static String firstLineOfFile(String path) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader(path))){
            return br.readLine();
        }
    }

    public static void main(String[] args) throws IOException {
        String path = args[0];
        System.out.println(firstLineOfFile(path));
    }
}

์ด ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package chapter01.item09.trywithresources;

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

public class TopLine {
    public TopLine() {
    }

    static String firstLineOfFile(String path) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));

        String var2;
        try {
            var2 = br.readLine();
        } catch (Throwable var5) {
            try {
                br.close();
            } catch (Throwable var4) {
                var5.addSuppressed(var4);
            }

            throw var5;
        }

        br.close();
        return var2;
    }

    public static void main(String[] args) throws IOException {
        String path = args[0];
        System.out.println(firstLineOfFile(path));
    }
}

try-finally๊ฐ€ ์•„๋‹Œ try-catch๋ฅผ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

๋˜ํ•œ ์˜ˆ์™ธ ์ •๋ณด๋ฅผ ๋‹ค ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด์„œ addSuppressed ๋กœ ์˜ˆ์™ธ ๋’ค์— ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ํ˜•์‹์ด๋‹ค. 

๊ทธ๋ฆฌ๊ณ  br.close()๋ฅผ ๋‘๋ฒˆ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ๋งˆ์ง€๋ง‰์—” ํ˜น์‹œ ๋ชฐ๋ผ์„œ ํ•œ๋ฒˆ ๋” ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ž์› ํ•ด์ œ๋ฅผ ํ•ด์ค€๋‹ค. -> ์ด๋•Œ๊ฐ™์€ ์ƒํ™ฉ์„ ์œ„ํ•ด์„œ AutoCloseable์ด๋‚˜ ์ด๋ฅผ ๊ตฌํ˜„ํ•œ Closeable์ด idemponent ํ•ด์•ผ ํ•˜๋Š”๊ฒƒ ๊ฐ™๋‹ค. ๋ช‡๋ฒˆ์„ ํ˜ธ์ถœํ•˜๋”๋ผ๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•˜๋‹ˆ๊นŒ! 

 

 

 

๐Ÿฅ try-finally๋กœ ํ•˜๋Š” ์ž์› ํ•ด์ œ๋Š” ์–ด๋”˜๊ฐ€ ํ•จ์ •์ด ์žˆ์„ ํ™•๋ฅ ์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค try-finally๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด 

์ด์ œ try-with-resources๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ๊ฒ ๋‹ค!!!! 

 

 

 

 

 

 

 

 

728x90