Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8266019

StreamResult(File) writes to incorrect file path if # is part of the file path

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      StreamResult(File) writes to incorrect file path if # is part of the file path

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See test case below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Write file ./output/#/dom.xml
      ACTUAL -
      Write file ./output

      Exception in thread "main" javax.xml.transform.TransformerException: java.io.FileNotFoundException: ./output (Is a directory)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(TransformerImpl.java:540)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:354)
      at test.Main.main(Main.java:26)
      Caused by: java.io.FileNotFoundException: ./output (Is a directory)
      at java.base/java.io.FileOutputStream.open0(Native Method)
      at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
      at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
      at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(TransformerImpl.java:513)
      ... 2 more

      ---------- BEGIN SOURCE ----------

      package test;

      import java.io.File;

      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.transform.OutputKeys;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.transform.stream.StreamResult;

      import org.w3c.dom.Document;

      public class Main {

      public static void main(String[] args) throws Exception {
      File f = new File("./output/#/dom.xml");
      f.getParentFile().mkdirs();

      Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
      dom.appendChild(dom.createElement("root"));

      Transformer tr = TransformerFactory.newInstance().newTransformer();
      tr.setOutputProperty(OutputKeys.INDENT, "yes");
      tr.transform(new DOMSource(dom), new StreamResult(f));
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use StreamResult(OutputStream) with standard FileOutputStream instead.

      FREQUENCY : always


            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: