java
try with resource 구문 예제 (JAVA 7)
easy16
2019. 9. 24. 23:15
try with resource 구문 예제
ex)
public class Java7Test {
public Java7Test() {
this.tryWithResource();
}
private void tryWithResource() {
String fullPath = "C:"+File.separator+"Users"+File.separator+"Lee"+File.separator+"test"+File.separator+"nio.txt";
//Closeable을 구현한 객체는 close()를 호출하지 않아도 알아서 해제해 준다.
try(Scanner scanner = new Scanner(new File(fullPath))) {
System.out.println(scanner.nextLine());
}catch( NullPointerException | IOException e){
//catch문 내에 예외 처리문이 같은 예외들을 Pipe로 묶어 한번에 처리 가능하다.
e.printStackTrace();
}
}
}
출처 : 자바의 신