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