try-with-resources문

2022. 12. 8. 00:06자바

728x90

<자동으로 파일 닫기>

try(FileInputStream fin = new FileInputStream(args[0]))//괄호 안에 사용할 자원 초기화
{...
} catch(IOException e) {
System.out.println("An I/O Error Occurred");
}

try 블록이 끝난 후 자원이 자동으로 릴리즈 됨

 

try ( FileInputStream fin = new FileInputStream(args[0]);
FileOutputStream fout = new FileOutputStream(args[1]) )
//두 개 이상의 자원을 명시 할 때는 세미콜론으로 구분
{...
}
catch(IOException e) {
System.out.println("I/O Error: " + e);
}

 

728x90

'자바' 카테고리의 다른 글

제네릭  (0) 2022.12.09
기타 자바 키워드  (0) 2022.12.08
입출력 기본  (0) 2022.12.07
열거형, 박싱, 어노테이션  (0) 2022.12.07
디폴트 메서드  (0) 2022.12.07