メインコンテンツまでスキップ

Input-Output연습

파일 인풋 / 아웃풋 스트림 코드 연습과 버퍼. 그리고 사용 예제를 직접 쳐보면서 익숙해지자


    public static void main(String[] args) {

FileInputStream in = null;
try {
in = new FileInputStream("sample.txt");
// FileInputStream 으로부터 읽어들여서 처리
} finally {
if (in !=null) {
try {
in.close();
} catch (Exception ex) {
// close시에 발생하는 예외 처리
}
}
}
}

1.6까지는 이렇게 try~finally로 해줘야 했다.

1.7 이후부터는 이렇게 try-with-resource구문으로 간단하게 해결 할 수 있다.

try(FileInputStream in = new FileInputStream("sample.txt")) {
// FileInputStream 으로부터 읽어들여서 처리
}