Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[udig-devel] Re: [Geotools-devel] Converting java.net.URLs to java.io.Files

Yes, going from File to URL is easy. The problem is going from URL to File.

On Thu, 21 Jun 2007 19:18:49 -0700
Jody Garnett <jgarnett@xxxxxxxxxxxxxxx> wrote:

> I just found that File.toURL() is deprecated (in Java 6):
> > @deprecated This method does not automatically escape characters that
> >      * are illegal in URLs.  It is recommended that new code convert an
> >      * abstract pathname into a URL by first converting it into a URI, 
> > via the
> >      * {@link #toURI() toURI} method, and then converting the URI into 
> > a URL
> >      * via the {@link java.net.URI#toURL() URI.toURL} method.
> I wonder if this technique, file.toURI().toURL(), behaves a bit better 
> then what you are hacking around?
> Jody
> 
> > I am doing testing against trying to view/edit/manipulate files that reside on a windows network share, and I have run into several instances across both uDig and GeoTools where java.net.URLs are (improperly?) converted into java.io.Files. The usual method of doing this is to just use URL.getPath:
> >
> > File f = new File(url.getPath());
> >
> > But if URL equals "file://E:/somedir/foo.tif", then File f is associated with the file "\somedir\foo.tif". The drive/network share information is lost.
> >
> > The authority property contains this information:
> >
> > String auth = url.getAuthority();
> > if (auth != null && !auth.equals("")) {
> >   f = new File("//"+auth+url.getPath());
> > } else {
> >   f = new File(url.getPath());
> > }
> >
> > The variable 'auth' equals "E:" in this case, or "fooServer" if it is a network share. In uDig I have added a utility method to the URLUtils class. If someone wants this in GeoTools, tell me where to put it and I will. 
> >
> > It is a fair assumption that this happens a fair amount in our code, and I don't have time to go through the code base to check for instances of URL.getPath(), so I would just like to bring it to everyone's attention to keep an eye out for this.
> >
> > Of course, if anyone has any better suggestions, please let me know. But for now I am fixing it in the image module, and I may fix it in shapefile next.
> >
> > Richard
> >
> >
> > -------------------------------------------------------------------------
> > This SF.net email is sponsored by DB2 Express
> > Download DB2 Express C - the FREE version of DB2 express and take
> > control of your XML. No limits. Just data. Click to get it now.
> > http://sourceforge.net/powerbar/db2/
> > _______________________________________________
> > Geotools-devel mailing list
> > Geotools-devel@xxxxxxxxxxxxxxxxxxxxx
> > https://lists.sourceforge.net/lists/listinfo/geotools-devel
> >   
> 


Back to the top