IOUtils.closeQuietly

IOUtils.closeQuietly() 它将无条件的关闭一个可被关闭的对象而不抛出任何异常。

String filePath = "E:\\a.txt";
File file = new File(filePath);
if (file.exists()) {
     BufferedReader reader = null;
     try {
          InputStream ins = new FileInputStream(file);
          reader = new BufferedReader(
                        new InputStreamReader(ins));
          String str;
          while ((str = reader.readLine()) != null) {
               String[] arr = StringUtils.split(str, " ");
               if (arr != null && arr.length == 2) {
                   System.out.println(arr[0]+" "+arr[1]);
              }
          }
      } catch (IOException e) {
          log.error("exception", e);
      } finally {
          IOUtils.closeQuietly(reader);
      }
}

如果直接用

reader.close();

需要添加 try catch IOException

 

posted @ 2020-04-01 17:18  慕尘  阅读(4695)  评论(0编辑  收藏  举报