title: Java: how to get a File from an escaped URL?
body: <p>I'm receiving an URL that locates a local file (the fact that I receive an URL is not in my control).  The URL is escaped validly as defined in RFC2396.  How can I transform this to a Java File object?</p>
  
  <p>Funnily enough, the URL getFile() method returns a String, not a File.</p>
  
  <p>I've created a directory called "/tmp/some dir" (with a spacing character between "some" and "dir"), which is correctly located by the following URL: "file:///tmp/some%20dir"  (quotes added for clarity).</p>
  
  <p>How can I convert that URL to a Java File?</p>
  
  <p>To give more detail about my issue, the following prints false:</p>
  
  <pre><code>URL url = new URL( "file:///tmp/some%20dir" );
  File f = new File( url.getFile() );
  System.out.println( "Does dir exist? " + f.exists() );
  </code></pre>
  
  <p>While the following (manually replacing "%20" with a space) prints true:</p>
  
  <pre><code>URL url = new URL( "file:///tmp/some%20dir" );
  File f = new File( url.getFile().replaceAll( "%20", " " ) );
  System.out.println( "Does dir exist? " + f.exists() );
  </code></pre>
  
  <p>Note that I'm not asking <em>why</em> the first example prints false nor <em>why</em> the second example using my hacky <em>replaceAll</em> prints true, I'm asking how to convert an escaped URL into a Java File object.</p>
  
  <p><strong>EDIT</strong>: thanks all, this was nearly a dupe but not exactly.</p>
  
  <p>Stupidly I was looking for a helper method inside the URL class itself.</p>
  
  <p>The following works as expected to get a Java File from a Java URL:</p>
  
  <pre><code>URL url = new URL( "file:///home/nonet/some%20dir" );
  File f = new File( URLDecoder.decode( url.getFile(), "UTF-8" ) );
  </code></pre>