[Java] try-with-resources

๋ฌด1๋ฏผยท2023๋…„ 11์›” 20์ผ
0

java

๋ชฉ๋ก ๋ณด๊ธฐ
4/4
post-thumbnail

๐ŸŽถ try-with-resources?

Java 7๋ฒ„์ „ ์ด์ „์—๋Š” ๋‹ค ์‚ฌ์šฉํ•˜๊ณ  ๋‚œ ์ž์›(resource)๋ฅผ ๋ฐ˜๋‚ฉํ•˜๊ธฐ ์œ„ํ•ด try-catch-finally๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
Java 7๋ฒ„์ „ ์ดํ›„์— ์ถ”๊ฐ€๋œ try-with-resources ๊ธฐ๋Šฅ์€ try ๊ตฌ๋ฌธ์— ๋ฆฌ์†Œ์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ , ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ค ์‚ฌ์šฉํ•˜๊ณ  ๋‚˜๋ฉด ์ž๋™์œผ๋กœ ๋ฐ˜๋‚ฉํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

๐Ÿ’โ€โ™€๏ธtry-catch-finally ๊ตฌ๋ฌธ

์ด์ „์— ์‚ฌ์šฉํ–ˆ๋˜ try-catch-finally ๊ตฌ๋ฌธ์„ ์•Œ์•„๋ณด๊ฒ ๋‹ค.

import java.io.File;import java.io.FileNotFoundException;
import java.util.Scanner;
public class ResourceClose {    
	public static void main(String[] args) {        
    	Scanner scanner = null;        
        try {            
        	// scanner ์ƒ์„ฑ            
            scanner = new Scanner(new File("input.txt"));
            System.out.println(scanner.nextLine());        
        } catch (FileNotFoundException e) {            
        	e.printStackTrace();        
        } finally {            
        	// scanner ๋ฆฌ์†Œ์Šค ๋ฐ˜๋‚ฉ            
            if (scanner != null) {                
            scanner.close();            
            }        
        }    
    }
}

๋ฆฌ์†Œ์Šค ์ƒ์„ฑ์€ try ๊ตฌ๋ฌธ์—์„œ, ๋ฆฌ์†Œ์Šค ๋ฐ˜๋‚ฉ์€ finally์—์„œ ์‹œํ–‰ํ•œ ๋ชจ์Šต์ด๋‹ค.

๐Ÿ™…โ€โ™‚๏ธtry-catch-finally ๋‹จ์ 

  • ์ž์› ๋ฐ˜๋‚ฉ์— ์˜ํ•ด ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง
  • ์ž‘์—…์ด ๋ฒˆ๊ฑฐ๋กœ์›€
  • ์‹ค์ˆ˜๋กœ ์ž์›์„ ๋ฐ˜๋‚ฉํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒ
  • ์—๋Ÿฌ๋กœ ์ž์›์„ ๋ฐ˜๋‚ฉํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒ
  • ์—๋Ÿฌ ์Šคํƒ ํŠธ๋ ˆ์ด์Šค๊ฐ€ ๋ˆ„๋ฝ๋˜์–ด ๋””๋ฒ„๊น…์ด ์–ด๋ ค์›€

=> ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด try-with-resources๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

๐Ÿ’โ€โ™‚๏ธtry-with-resources ๊ตฌ๋ฌธ

์ด์ œ try-with-resources ๊ตฌ๋ฌธ์„ ์•Œ์•„๋ณด๊ฒ ๋‹ค.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; 
public class ResourceClose {    
	public static void main(String[] args) {        
    	try (Scanner scanner = new Scanner(new File("input.txt"))) {            		System.out.println(scanner.nextLine());        
    	} catch (FileNotFoundException e) {            
    		e.printStackTrace();        
    	}    
  	}
}

try ์˜†์— ๊ด„ํ˜ธ์•ˆ์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.
๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ž๋™์œผ๋กœ ๋ฐ˜๋‚ฉํ•œ๋‹ค.
AutoClosable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” ์ž์›์— ๋Œ€ํ•ด try-with-resources๋ฅผ ์ ์šฉ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜์˜€๋‹ค.

๐Ÿคทโ€โ™‚๏ธClosable๊ณผ AutoClosable์˜ ๊ด€๊ณ„

์‹ ๊ธฐํ•œ ์ ์€ ๊ธฐ์กด์˜ Closable์— ๋ถ€๋ชจ ์ธํ„ฐํŽ˜์ด์Šค๋กœ AutoClosable์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

์ด๋กœ์จ, ํ•˜์œ„ ํ˜ธํ™˜์„ฑ์„ 100% ๋‹ฌ์„ฑํ•จ๊ณผ ๋™์‹œ์— ๋ณ€๊ฒฝ ์ž‘์—…์— ๋Œ€ํ•œ ์ˆ˜๊ณ ๊ฐ€ ์ค„์—ˆ๋‹ค.

๐Ÿ‘Š์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ ๋ฐ˜๋‚ฉ

์•„๋ž˜์™€ ๊ฐ™์ด ;๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner; 
public class ResourceClose {    
	public static void main(String[] args) {        
    	try (Scanner scanner = new Scanner(new File("input.txt"));
        	PrintWriter pw = new PrintWriter(new File("output.txt"))) {
        	System.out.println(scanner.nextLine());        
        } catch (FileNotFoundException e) {
        	e.printStackTrace();
    	}    
	}
}
profile
์•ผํ˜ธ

0๊ฐœ์˜ ๋Œ“๊ธ€