이펙티브 코틀린 아이템 9: use를 사용해 리소스를 닫아라
이펙티브 코틀린(안정성)
아이템 9: use를 사용해 리소스를 닫아라
Closeable를 구현하고 있는 클래스들을 처리 할때 try finally 를 사용해서 close 메소드를 호출
java의 try with resource 대신 use 와 Reader.useLines 확장 메소드를 제공한다.
fun countCharactersInFile(path: String): Int {
BufferedReader(FileReader(path)).use {
return reader.lineSequence().sumBy { it.length }
}
}
fun countCharactersInFile(path: String): Int {
File(path).useLines { lines ->
lines.sumBy { it.length }
}
}