<legend id='itEiy'><style id='itEiy'><dir id='itEiy'><q id='itEiy'></q></dir></style></legend>
    1. <i id='itEiy'><tr id='itEiy'><dt id='itEiy'><q id='itEiy'><span id='itEiy'><b id='itEiy'><form id='itEiy'><ins id='itEiy'></ins><ul id='itEiy'></ul><sub id='itEiy'></sub></form><legend id='itEiy'></legend><bdo id='itEiy'><pre id='itEiy'><center id='itEiy'></center></pre></bdo></b><th id='itEiy'></th></span></q></dt></tr></i><div id='itEiy'><tfoot id='itEiy'></tfoot><dl id='itEiy'><fieldset id='itEiy'></fieldset></dl></div>

      <small id='itEiy'></small><noframes id='itEiy'>

      <tfoot id='itEiy'></tfoot>

        <bdo id='itEiy'></bdo><ul id='itEiy'></ul>

      1. 声纳:如何使用 try-with-resources 关闭 FileOutputStream

        时间:2024-05-09
        <legend id='UL68m'><style id='UL68m'><dir id='UL68m'><q id='UL68m'></q></dir></style></legend><tfoot id='UL68m'></tfoot>

          <i id='UL68m'><tr id='UL68m'><dt id='UL68m'><q id='UL68m'><span id='UL68m'><b id='UL68m'><form id='UL68m'><ins id='UL68m'></ins><ul id='UL68m'></ul><sub id='UL68m'></sub></form><legend id='UL68m'></legend><bdo id='UL68m'><pre id='UL68m'><center id='UL68m'></center></pre></bdo></b><th id='UL68m'></th></span></q></dt></tr></i><div id='UL68m'><tfoot id='UL68m'></tfoot><dl id='UL68m'><fieldset id='UL68m'></fieldset></dl></div>

          <small id='UL68m'></small><noframes id='UL68m'>

              <bdo id='UL68m'></bdo><ul id='UL68m'></ul>
                  <tbody id='UL68m'></tbody>

                1. 本文介绍了声纳:如何使用 try-with-resources 关闭 FileOutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Sonar 给出了一个错误,提示该 FileOutputStream 应该被关闭.我需要修改以下代码以使用 try-with-resources.我该怎么做?

                  Sonar is giving an error that this FileOutputStream should be closed. I need to modify the following code to use try-with-resources. How do I do this?

                  public void archivingTheFile(String zipFile){
                      byte[] buffer = new byte[1024];
                      try{
                          FileOutputStream fos = new FileOutputStream(zipFile);
                          ZipOutputStream zos = new ZipOutputStream(fos);
                          for(String file : this.fileList){
                              ZipEntry ze= new ZipEntry(file);
                              zos.putNextEntry(ze);
                              FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
                              int len;
                              while ((len = in.read(buffer)) > 0) {
                                  zos.write(buffer, 0, len);
                              }
                              in.close();
                          }
                          zos.closeEntry();
                          zos.close();
                      }catch(IOException ex){
                          LOGGER.error("Exception occurred while zipping file",ex);
                      }
                  }
                  

                  推荐答案

                  当前代码还没有准备好处理异常 - 你缺少 finally 块来关闭打开的流.而且,当然,你是对的 - 使用 try-with-resources 解决了这个问题:

                  Currently code is not ready to handle exceptions - you're missing finally block to close open streams. And, sure, you're right - using try-with-resources solves this problem:

                  public void archivingTheFile(String zipFile) {
                      byte[] buffer = new byte[1024];
                      try (FileOutputStream fos = new FileOutputStream(zipFile);
                           ZipOutputStream zos = new ZipOutputStream(fos)) {
                          for(String file : this.fileList) {
                              try (FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file)) {
                                  ZipEntry ze = new ZipEntry(file);
                                  zos.putNextEntry(ze);
                                  int len;
                                  while ((len = in.read(buffer)) > 0) {
                                      zos.write(buffer, 0, len);
                                  }
                              }
                          }
                      } catch(IOException ex) {
                          LOGGER.error("Exception occurred while zipping file",ex);
                      }
                  }
                  

                  这篇关于声纳:如何使用 try-with-resources 关闭 FileOutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:枚举的字段是否必须是可序列化的? 下一篇:鱿鱼:Android 上的最大继承深度

                  相关文章

                2. <tfoot id='DkJM6'></tfoot><legend id='DkJM6'><style id='DkJM6'><dir id='DkJM6'><q id='DkJM6'></q></dir></style></legend>
                      <bdo id='DkJM6'></bdo><ul id='DkJM6'></ul>
                    <i id='DkJM6'><tr id='DkJM6'><dt id='DkJM6'><q id='DkJM6'><span id='DkJM6'><b id='DkJM6'><form id='DkJM6'><ins id='DkJM6'></ins><ul id='DkJM6'></ul><sub id='DkJM6'></sub></form><legend id='DkJM6'></legend><bdo id='DkJM6'><pre id='DkJM6'><center id='DkJM6'></center></pre></bdo></b><th id='DkJM6'></th></span></q></dt></tr></i><div id='DkJM6'><tfoot id='DkJM6'></tfoot><dl id='DkJM6'><fieldset id='DkJM6'></fieldset></dl></div>

                      <small id='DkJM6'></small><noframes id='DkJM6'>