title: How to avoid java.net.URISyntaxException in URL.toURI()
body: <p>In a particular program, I am passed a <code>file:</code> URL and I need to convert it to a <code>URI</code> object.  Using the <code>toURI</code> method will throw a <code>java.net.URISyntaxException</code> if there are spaces or any other invalid characters in the URL.</p>
  
  <p>For example:</p>
  
  <pre><code>URL url = Platform.getInstallURL();  // file:/Applications/Program
  System.out.println(url.toURI());  // prints file:/Applications/Program
  
  URL url = Platform.getConfigurationURL();  // file:/Users/Andrew Eisenberg
  System.out.println(url.toURI());  // throws java.net.URISyntaxException because of the space
  </code></pre>
  
  <p>What is the best way of performing this conversion so that all special characters are handled?</p>