#vulnerable code 
                                void prepareVizHeader() {

        PrintWriter printWriter;

        try {
            printWriter = new PrintWriter(new FileOutputStream(visFilename));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            printWriter = new PrintWriter(new StringWriter());
        }

        StreamResult streamResult = new StreamResult(printWriter);
        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

        try {

            hdVis = tf.newTransformerHandler();

            Transformer serializer = hdVis.getTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // "ISO-8859-1");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.STANDALONE, "yes");
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

            hdVis.setResult(streamResult);

            hdVis.startDocument();

            AttributesImpl atts = new AttributesImpl();
            atts.addAttribute("", "", "version", "CDATA", "1.0");
            atts.addAttribute("", "", "xmlns:xsi", "CDATA", "http://www.w3.org/2001/XMLSchema-instance");
            atts.addAttribute("", "", "xsi:noNamespaceSchemaLocation", "CDATA", "visualization.xsd");

            String ourText = " Generated by JaCoP solver; " + getDateTime() + " ";
            char[] comm = ourText.toCharArray();
            hdVis.comment(comm, 0, comm.length);

            hdVis.startElement("", "", "visualization", atts);

            // visualizer
            if (tracedVar.size() != 0) {
                AttributesImpl visAtt = new AttributesImpl();
                visAtt.addAttribute("", "", "id", "CDATA", "1");
                visAtt.addAttribute("", "", "type", "CDATA", "vector");
                visAtt.addAttribute("", "", "display", "CDATA", "expanded");
                visAtt.addAttribute("", "", "group", "CDATA", "default");
                visAtt.addAttribute("", "", "x", "CDATA", "0");
                visAtt.addAttribute("", "", "y", "CDATA", "0");

                int minV = minValue(tracedVar), maxV = maxValue(tracedVar);
                visAtt.addAttribute("", "", "width", "CDATA", "" + tracedVar.size());
                visAtt.addAttribute("", "", "height", "CDATA", "" + (int) (maxV - minV + 1));

                visAtt.addAttribute("", "", "min", "CDATA", "" + minV);
                visAtt.addAttribute("", "", "max", "CDATA", "" + maxV);

                hdVis.startElement("", "", "visualizer", visAtt);
                hdVis.endElement("", "", "visualizer");
            }

            generateVisualizationNode(0, true);


        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
                            #location 12 
                            #vulnerability type RESOURCE_LEAK
#fixed code
                                void prepareTreeHeader() {

        OutputStreamWriter printWriter;

        try {
            printWriter = new OutputStreamWriter(
                new FileOutputStream(treeFilename),
                Charset.forName("UTF-8").newEncoder()
            );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            printWriter = new OutputStreamWriter(
                System.out,
                Charset.forName("UTF-8").newEncoder()
            );
        }

        StreamResult streamResult = new StreamResult(printWriter);
        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

        try {

            hdTree = tf.newTransformerHandler();

            Transformer serializer = hdTree.getTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // "ISO-8859-1");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.STANDALONE, "yes");
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

            hdTree.setResult(streamResult);

            hdTree.startDocument();

            AttributesImpl atts = new AttributesImpl();
            atts.addAttribute("", "", "version", "CDATA", "1.0");
            atts.addAttribute("", "", "xmln:xsi", "CDATA", "http://www.w3.org/2001/XMLSchema-instance");
            atts.addAttribute("", "", "xsi:noNamespaceSchemaLocation", "CDATA", "tree.xsd");

            String ourText = " Generated by JaCoP solver; " + getDateTime() + " ";
            char[] comm = ourText.toCharArray();
            hdTree.comment(comm, 0, comm.length);

            hdTree.startElement("", "", "tree", atts);

            atts = new AttributesImpl();
            atts.addAttribute("", "", "id", "CDATA", "0");
            hdTree.startElement("", "", "root", atts);
            hdTree.endElement("", "", "root");

        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }