your programing

Groovy, "리소스 활용"건설 대안

lovepro 2020. 12. 30. 19:52
반응형

Groovy, "리소스 활용"건설 대안


저는 Groovy를 처음 사용합니다. 저는 I / O 스트림 작업 중에 Java 코드에서 'try-with-resources'구성을 사용했습니다.

조언 해 주시겠습니까, Groovy에서 그러한 구조의 유사점이 있습니까?


문서Groovy IO 및 관련 javadoc을 살펴보십시오 . 자동 닫기 기능으로 스트림을 가져 오는 수단 인 ,, 구성을 제공합니다 withStream.withWriterwithReader


Groovy 2.3은 또한 CloseablewithCloseable 을 구현하는 모든 것에서 작동합니다.


모든 Groovy 버전에 대한 가장 간단한 try-with-resources는 다음과 같습니다 ( AutoCloseable인터페이스 에서도 작동 함 ). 클래스 Thing가 닫을 수있는 클래스이거나 AutoCloseable.

new Thing().with { res ->
    try {
        // do stuff with res here
    } finally {
        res.close()
    }
}

Groovy의 이후 버전에서 다음을 수행하는 것과 같습니다.

new Thing().withCloseable { res ->
    // do stuff with res here
}

참조 URL : https://stackoverflow.com/questions/23382079/groovy-try-with-resources-construction-alternative

반응형