OpenOption — 파일 열기 옵션 인터페이스

OpenOption — 파일 열기 옵션 인터페이스

파일을 열거나 생성할 때 어떻게 다룰지 설정하는 객체를 나타내는 인터페이스예요. 이 타입의 객체는 newOutputStream, newByteChannel, FileChannel.open, AsynchronousFileChannel.open 같은 메서드가 파일을 열거나 생성할 때 사용해요. 표준 옵션은 StandardOpenOption 열거형이 정의해요.

출처: Java API Reference

본문

이 인터페이스는 마커(marker) 인터페이스로, 파일을 열 때의 동작(읽기·쓰기·추가·새로 만들기·잘라내기 등)을 나타내는 옵션 객체의 공통 타입이에요. 직접 구현하기보다는 StandardOpenOption의 상수를 사용하는 것이 일반적이에요.

// 읽기 전용으로 파일 채널 열기
FileChannel.open(path, StandardOpenOption.READ);

// 없으면 만들고, 있으면 덮어쓰기
FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE);

더 알아보기 (Learn more)