public class URLs extends Object
URLs.
urlToFile(URL) and fileToUrl(File) and getParentUrl(URL) are used
to work with files across platforms
| Constructor and Description |
|---|
URLs() |
| Modifier and Type | Method and Description |
|---|---|
static URL |
changeUrlExt(URL url,
String postfix)
Changes the ending (e.g. ".sld") of a
URL |
static URL |
extendUrl(URL base,
String extension)
Extends a
URL. |
static URL |
fileToUrl(File file)
A replacement for
File.toURL() and File.toURI().toURL(). |
static URL |
getParentUrl(URL url)
The function is supposed to be equivalent to
File.getParent(). |
static File |
urlToFile(URL url)
Takes a URL and converts it to a File.
|
public static URL changeUrlExt(URL url, String postfix) throws IllegalArgumentException
URLurl - URL like file:/sds/a.bmp or
http://www.some.org/foo/bar.shppostfix - New file extension for the URL without .URL with new extension.{@link - MalformedURLException} if the new URL can not be created.IllegalArgumentExceptionpublic static URL extendUrl(URL base, String extension) throws MalformedURLException
URL.base - Has to be a URL pointing to a directory. If it doesn't end with a /
it will be added automatically.extension - The part that will be added to the URLMalformedURLException - if the new URL can not be created.public static URL fileToUrl(File file)
File.toURL() and File.toURI().toURL().
File.toURL() does not percent-encode characters and File.toURI().toURL()
does not percent-encode non-ASCII characters. This method ensures that URL characters
are correctly percent-encoded, and works around the reported misbehaviour of some Java
implementations on Mac.
file - public static URL getParentUrl(URL url) throws MalformedURLException
File.getParent(). The URL is
converted to a String, truncated to the last / and then recreated as a new URL.{@link - MalformedURLException} if the parent URL can not be created.MalformedURLExceptionpublic static File urlToFile(URL url)
If the URL.getAuthority() returns null or is empty, then only the url's path property is used to construct the file. Otherwise, the authority is prefixed before the path.
It is assumed that url.getProtocol returns "file".
Authority is the drive or network share the file is located on. Such as "C:", "E:", "\\fooServer"
url - a URL object that uses protocol "file"Copyright © 1996–2019 Geotools. All rights reserved.