Java Disposable pattern

前端 未结 3 1616

C# supports disposable pattern for deterministic garbage collection using the dispose pattern.

Is there such pattern for java?

Java 7 has autoclosable, whic

3条回答
  •  天涯浪人
    2021-01-17 11:49

    What you are looking for is try with resources.

    try ( FileInputStream input = new FileInputStream(...);
          BufferedReader br = new BufferedReader(...) ) {
      // Use input
    } 
    

    The resource has to be Closeable (or AutoCloseable), of course.

提交回复
热议问题