[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item9. try-finally ๋ณด๋‹ค๋Š” try-with-resources ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.

2023. 1. 25. 14:33ใ†JAVA/Effective JAVA

728x90

 

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

 

https://hyejin.tistory.com/1011

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item8. finalizer์™€ cleaner ์‚ฌ์šฉ์„ ํ”ผํ•˜๋ผ.

item07. ๋‹ค ์“ด ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ผ. ์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” part 1์—์„œ๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ์— ๋Œ€ํ•ด์„œ ์–˜๊ธฐํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ง€๊ธˆ์€ ์ €๋ฒˆ item ๋ถ€ํ„ฐ๋Š” ๊ฐ์ฒด ์†Œ๋ฉธ์— ๋Œ€ํ•ด์„œ ์ด์•ผ๊ธฐ ํ•˜๊ณ  ์žˆ๋‹ค. ์šฐ์„  ์ž๋ฐ”์—์„œ๋Š” ๋‘

hyejin.tistory.com

 

์—ฌ๊ธฐ์„œ ๋‚ด์šฉ์ด ์ถ”๊ฐ€๋œ? ๋Š๋‚Œ์œผ๋กœ Item09. ์—์„œ๋„ try-with-resources ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค. (๊ถŒ์žฅ๋ณด๋‹ค๋Š” ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•˜๊ณ  ์žˆ๋‹ค.) 

 

 

์ž๋ฐ”์—์„œ๋Š” close ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ง์ ‘ ๋‹ซ์•„์ค˜์•ผํ•˜๋Š” ์ž์›์ด InputStream, OutputStream, Connection ๋“ฑ์ด ์žˆ๋‹ค. 

์ž์› ๋‹ซ๊ธฐ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋†“์น˜๊ธฐ ์‰ฌ์šด ๋ถ€๋ถ„์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋Š” ์„ฑ๋Šฅ ๋ฌธ์ œ๋กœ ์ด์–ด์ง€๊ธฐ๋„ ํ•œ๋‹ค.. 

์˜ˆ์ „์—๋Š” ์ž์›์ด ์ œ๋Œ€๋กœ ๋‹ซํž˜์„ ๋ณด์žฅํ•˜๋Š” ์ˆ˜๋‹จ์œผ๋กœ try-finally๊ฐ€ ๋งŽ์ด ์“ฐ์˜€๋Š”๋ฐ ์ž๋ฐ”7์ด์ƒ๋ถ€ํ„ฐ๋Š” ๋”์ด์ƒ try-finally๊ฐ€ ์ตœ์„ ์˜ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋‹ค. 

 

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

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

์ด๋ ‡๊ฒŒ ์˜ˆ์ „์—๋Š” ์ž์›์ด ์ œ๋Œ€๋กœ ๋‹ซํž˜์„ ๋ณด์žฅํ•˜๋Š” ์ˆ˜๋‹จ์œผ๋กœ ๋งŽ์ด ์ผ์—ˆ๋Š”๋ฐ ์ด ์ฝ”๋“œ๊ฐ€ ํ‹€๋ ธ๋‹ค๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ 

๋งŒ์•ฝ์— ์ž์›์„ ํ•œ๊ฐœ๊ฐ€ ์•„๋‹Œ ๋‘๊ฐœ ์ด์ƒ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ? 

 

package chapter01.item09.tryfinally;

import java.io.*;

/**
 * item09. try-finally๋ณด๋‹ค try-with-resources๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.
 */
public class Copy {

    private static final int BUFFER_SIZE = 8 * 1024;

    // ์ž์›์ด ๋‘˜ ์ด์ƒ์ด๋ฉด try-finally ๋ฐฉ์‹์€ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ์ง€์ €๋ถ„ํ•ด์ง„๋‹ค.
    static void copy(String src, String dst) throws IOException {
        InputStream in = new FileInputStream(src);
        try {
            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 {
                out.close();
            }
        }finally {
            in.close();
        }
    }

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

-> ์ž์›์ด ๋‘๊ฐœ ์ด์ƒ์ด ๋˜๋‹ˆ๊นŒ try-finally ๋ฐฉ์‹์€ ์ฝ”๋“œ๊ฐ€ ๊ต‰์žฅํžˆ ์ง€์ €๋ถ„ํ•ด์ง„๋‹ค. 

 

 

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));
    }
}
package chapter01.item09.trywithresources;

import java.io.*;

/**
 * item09: try-finally ๋ณด๋‹ค๋Š” try-with-resources ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.
 */
public class Copy {
    private static final int BUFFER_SIZE = 8 * 1024;

    // try-with-resources๋Š” ๋ณต์ˆ˜์˜ ์ž์›์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๊ฒฐํ•˜๋‹ค.
    static void copy(String src, String dst) throws IOException {

        try (InputStream in = new FileInputStream(src);
             OutputStream out = new FileOutputStream(dst)
        ) {
            byte[] buf = new byte[BUFFER_SIZE];
            int n;
            while ((n = in.read(buf)) >= 0) {
                out.write(buf, 0, n);
            }
        }
    }

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

 

๊ทธ์— ๋น„ํ•ด try-with-resources๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด try-finally์— ๋น„ํ•ด ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค๋Š” ์žฅ์  ์™ธ์— ์˜ˆ์™ธ ์ •๋ณด์— ๊ด€ํ•œ ์žฅ์ ์ด ํ•˜๋‚˜ ๋” ์žˆ๋‹ค.

public class BadBufferedReader extends BufferedReader {
    public BadBufferedReader(Reader in, int sz) {
        super(in, sz);
    }

    public BadBufferedReader(Reader in) {
        super(in);
    }

    @Override
    public String readLine() throws IOException {
        throw new CharConversionException();
    }

    @Override
    public void close() throws IOException {
        throw new StreamCorruptedException();
    }
}

์šฐ์„  ์ด๋ ‡๊ฒŒ BufferedReader๋ฅผ ์ƒ์†๋ฐ›๊ณ , readLine ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด CharconversionException์ด ๋˜์ ธ์ง€๊ณ , close ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด StreamCorruptedException์ด ๋˜์ ธ์ง„๋‹ค๊ณ  ํ•œ๋‹ค. 

 

public class TopLine {
    static String firstLineOfFile(String path) throws IOException {
        BufferedReader br = new BadBufferedReader(new FileReader(path));
        // try-finally๋Š” ๋งจ ๋งˆ์ง€๋ง‰์— ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋งŒ ๋ณด์ธ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.
        // ์ฒซ๋ฒˆ์งธ ์˜ˆ์™ธ๊ฐ€ ์ค‘์š”ํ•œ๋ฐ ๋‹ค๋ฅธ ์˜ˆ์™ธ๋“ค์€ ๋ณด์ด์ง€ ์•Š๋Š” ๊ฒƒ์ด ๋‹จ์ ..!!
        try {
            return br.readLine();
        }finally {
            br.close();
        }

    }

    public static void main(String[] args) throws IOException{
        System.out.println(firstLineOfFile("book.obj"));
    }
}

try-finally ๋ฅผ ์‚ฌ์šฉํ•œ firstLineOfFile ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด 

readLine ์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋Š” ์™„์ „ํžˆ ์‚ผ์ผœ์ง€๊ณ  close ์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋งŒ ๋ณด์ด๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค..

์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์Šคํƒ ์ถ”์  ๋‚ด์—ญ์—์„œ ์ฒซ๋ฒˆ์งธ ์˜ˆ์™ธ์— ๊ด€ํ•œ ์ •๋ณด๋Š” ํ•˜๋‚˜๋„ ๋‚จ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ ์‹œ์Šคํ…œ์—์„œ ๋””๋ฒ„๊น…์„ ๊ต‰์žฅํžˆ ์–ด๋ ต๊ฒŒ ํ•œ๋‹ค..! 

๋ฌผ๋ก  ๋‘๋ฒˆ์งธ ์˜ˆ์™ธ์—์„œ ์ฒซ๋ฒˆ์งธ ์˜ˆ์™ธ๋ฅผ ๊ธฐ๋กํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ๊ฐ€ ๊ต‰์žฅํžˆ ์ง€์ €๋ถ„ํ•ด ์ง€๊ณ , ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์—†๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

public class TopLineWithTryWithResources {

    // try-with-resources ๋Š” ์˜ˆ์™ธ๋ฅผ ๋ชจ๋‘ ๋ณด์—ฌ์ค€๋‹ค.
    static String firstLineOfFile(String path) throws IOException {
        try (BufferedReader bufferedReader = new BadBufferedReader(new FileReader(path))){
            return bufferedReader.readLine();
        }
    }

    public static void main(String[] args) throws IOException{
        System.out.println(firstLineOfFile("book.obj"));
    }
}

์ด๋ฒˆ์—๋Š” ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ try-with-resources ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๊ฒฐ๋œ๋‹ค. 

 

๐Ÿฅ ์ด์ „ item์—์„œ ๋ดค์ง€๋งŒ try-with-resources๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ์ž์›์ด AutoCloseable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. 

https://hyejin.tistory.com/1014

 

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

item08. finalizer์™€ cleaner ์‚ฌ์šฉ์„ ํ”ผํ•˜๋ผ. " p43. AutoClosable " AutoClosable์ด๋ž€? : try-with-resources๋ฅผ ์ง€์›ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค. /* * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/C

hyejin.tistory.com

 

try-with-resources๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด readLine ๊ณผ close ์—์„œ ๋ชจ๋‘ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๊ณ  ํ•˜๋ฉด readLine ๋ฉ”์„œ๋“œ์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๊ฐ€ ๊ธฐ๋ก๋˜๊ณ , close์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋Š” ์ˆจ๊ฒจ์ง€๋Š”๋ฐ ์ด๋Ÿฐ ์ˆจ๊ฒจ์ง„ ์˜ˆ์™ธ๋“ค์ด ๊ทธ๋ƒฅ ๋ฒ„๋ ค์ง€๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์Šคํƒ ์ถ”์  ๋‚ด์—ญ์—์„œ Suppressed ๋ผ๊ณ  ํ•ด์„œ ๊ผฌ๋ฆฌํ‘œ๋ฅผ ๋‹ฌ๊ณ  ์ถœ๋ ฅํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— 

์˜ˆ์™ธ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค!!! 

 

 

 

 

๐Ÿ’ก ๊ผญ ํšŒ์ˆ˜ํ•ด์•ผ ํ•˜๋Š” ์ž์›์ด ์žˆ์„ ๋•Œ ์ด์ œ๋Š” try-finally ๋Œ€์‹  try-with-resources๋ฅผ ์‚ฌ์šฉํ•˜์ž!! 

try-finally์— ๋น„ํ•ด ์ฝ”๋“œ๋„ ๋” ์งง๊ณ  ๋ถ„๋ช…ํ•˜๋ฉฐ, ๋งŒ๋“ค์–ด์ง€๋Š” ์˜ˆ์™ธ ์ •๋ณด๋„ ํ›จ์”ฌ ํ›จ์”ฌ ์œ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90