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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM