Transformer library that comes with Java converts spaces in file paths to %20
filepath, java, urlencode, xml
Solution
Try to change this line:
StreamResult result = new StreamResult(xmlFile);
into this:
StreamResult result = new StreamResult(new FileOutputStream(xmlFile));
I have no Idea, why the filename is handled as an URL.
Problem
Here is a test application that writes out XML Files. Why are the spaces in my path being converted to `%20`? ``` public class XmlTest { public static void main(String[] args) { String filename = "C:\\New Folder\\test.xml"; try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(doc); File xmlFile = new File(filename); if (xmlFile.exists()) { xmlFile.delete(); } StreamResult result = new StreamResult(xmlFile); transformer.transform(source, result); } catch (ParserConfigurationException ex) { ex.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } } } ``` Stacktrace: ``` java.io.FileNotFoundException: C:\New%20Folder\test.xml (The system cannot find the path specified) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:179) at java.io.FileOutputStream.<init>(FileOutputStream.java:70) at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:287) at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:330) at avm.trans.xml.XmlTest.main(XmlTest.java:52) ```