PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

在Java 9中如何在try-with-resources语句中声明多个资源?

王林
王林 转载
2023-08-25 22:57:02 768浏览

在Java 9中如何在try-with-resources语句中声明多个资源?

我们可以在try块中声明多个资源。try初始化块可以有任意数量的资源,这些资源可以是null或非null。

在下面的示例中,我们可以在try-with-resources语句中声明多个资源。

示例

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class MultipleResourcesTest {
   public static void main(String args[]) throws IOException {
      System.out.println(readData("test"));
   }
   static String readData(String message) throws IOException {
      try(Reader inputString = new StringReader(message);
      BufferedReader br = new BufferedReader(inputString)) {
         return br.readLine();
      }
   }
}

输出

test

以上就是在Java 9中如何在try-with-resources语句中声明多个资源?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除