Sintax blocking in SonarQube Use try-with-resources or close this "BufferedWriter" in a "finally" clause

0

When I upload my class and start the following analysis in SonarQube , it presents the following legend:

Use try-with-resources or close this "BufferedWriter" in a "finally" clause.

I really put the finally and still the blocker keeps dialing me, can someone tell me what I'm missing? because I have already put what he tells me and even then he marks me as a blocker. I leave my code where it is marked, it is in the section bw2 = new BufferedWriter(new FileWriter(source2));

public void escribeNuevoXML(ConsultaCatalogoBancoResponseBean response)throws Exception{

        LOGGER.info(":::Stating delete and writing new dates.....");

        int i =0;

         BufferedWriter bw = null;

         BufferedWriter bw2 = null;

        try{
        ClassLoader classLoader = getClass().getClassLoader();
           File source2 = new File (classLoader.getResource(catalogoBanco).getFile());

           bw = new BufferedWriter(new FileWriter(source2));

           bw.write("");

           bw2 = new BufferedWriter(new FileWriter(source2));

           bw2.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\n");

           bw2.write("<xml>\n");

           for(Banco banco: response.getListaBancos()){
                banco.setIdBanco(String.valueOf(i));
               bw2.write("\t<banco>\n");
               bw2.write("\t\t<cbe>"+banco.getCbe()+"</cbe>\n");
               bw2.write("\t\t<cveCECOBAN>"+banco.getCveCECOBAN()+"</cveCECOBAN>\n");
               bw2.write("\t\t<cbeMD>"+banco.getCbeMD()+"</cbeMD>\n");
               bw2.write("\t\t<tdd>"+banco.getTdd()+"</tdd>\n");
               bw2.write("\t\t<tdc>"+banco.getTdc()+"</tdc>\n");
               bw2.write("\t\t<claveSPEI>"+banco.getClaveSPEI()+"</claveSPEI>\n");
               bw2.write("\t\t<idBanco>"+banco.getIdBanco()+"</idBanco>\n");
               bw2.write("\t\t<tddDS>"+banco.getTddDS()+"</tddDS>\n");
               bw2.write("\t\t<cbeDS>"+banco.getCbeDS()+"</cbeDS>\n");
               bw2.write("\t\t<cveRegistro>"+banco.getCveRegistro()+"</cveRegistro>\n");
               bw2.write("\t\t<ocr>"+banco.getOcr()+"</ocr>\n");
               bw2.write("\t\t<tddMD>"+banco.getTddMD()+"</tddMD>\n");
               bw2.write("\t\t<sinApl>"+banco.getSinApl()+"</sinApl>\n");
               bw2.write("\t\t<nombreBanco>"+banco.getNombreBanco()+"</nombreBanco>\n");
               bw2.write("\t</banco>\n");
               bw2.flush();
                listaBancos.getCatalogoBancos().add(banco);
                LOGGER.info("Bank "+ banco.getIdBanco()+" "+banco.getNombreBanco());
            i++;
            }

        bw2.write("</xml>");

        bw2.flush();



        this.setBanderaBancos("false");

        LOGGER.info("LOADING LIST BANKS...");

        }catch(IOException e) {

            LOGGER.error("ANY PROBLEM HAS WRITING XML BANK ..."+ e.getMessage()+"\n"+e.getCause());

            ClassLoader classLoader = getClass().getClassLoader();
            File source=new File (classLoader.getResource(catalogoBanco).getFile());
            listaBancos = (ListaBancos) SerialicerXmlUtil.xmlToObject(source, ListaBancos.class);
            for(Banco banco: listaBancos.getCatalogoBancos()){

                LOGGER.info("Banco "+banco.getIdBanco()+" "+banco.getNombreBanco());
            }
        }finally {

            if(bw!= null) {
                bw.close();
            }
            if(bw2!=null) {
                bw2.close();
            }

        }
    }
    
asked by DANIEL 01.11.2018 в 16:01
source

0 answers