Java Utililty Methods URL to File Name

List of utility methods to do URL to File Name

Description

The list of methods to do URL to File Name are organized into topic(s).

Method

FilegetFile(URL aURL)
Returns the URL as a file.
if (aURL.getProtocol().equals("jar")) {
    String urls = aURL.toExternalForm();
    int sep = urls.indexOf('!');
    urls = urls.substring(4, sep);
    try {
        aURL = new URL(urls);
    } catch (Exception e) {
        throw new RuntimeException(e);
...
FilegetFile(URL resourceUrl, String description)
get File
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
    throw new FileNotFoundException(description + "->" + resourceUrl);
try {
    return new File(toURI(resourceUrl).getSchemeSpecificPart());
} catch (URISyntaxException ex) {
    return new File(resourceUrl.getFile());
FilegetFile(URL resourceUrl, String description)
Resolve the given resource URL to a java.io.File, i.e.
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
    throw new FileNotFoundException(description + " cannot be resolved to absolute file path "
            + "because it does not reside in the file system: " + resourceUrl);
try {
    return new File(resourceUrl.toURI().getSchemeSpecificPart());
} catch (URISyntaxException ex) {
    return new File(resourceUrl.getFile());
...
FilegetFile(URL resourceUrl, String description)
Resolve the given resource URL to a java.io.File, i.e.
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
    throw new FileNotFoundException(description + " cannot be resolved to absolute file path "
            + "because it does not reside in the file system: " + resourceUrl);
return new File(URLDecoder.decode(resourceUrl.getFile()));
FilegetFile(URL resourceUrl, String description)
get File
if (resourceUrl == null) {
    throw new IllegalArgumentException("Resource URL must not be null");
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
    throw new FileNotFoundException(description + " cannot be resolved to absolute file path "
            + "because it does not reside in the file system: " + resourceUrl);
try {
...
FilegetFile(URL resourceUrl, String description)
Resolve the given resource URL to a java.io.File, i.e.
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
    throw new FileNotFoundException(description + " cannot be resolved to absolute file path "
            + "because it does not reside in the file system: " + resourceUrl);
try {
    return new File(toURI(resourceUrl).getSchemeSpecificPart());
} catch (URISyntaxException ex) {
    return new File(resourceUrl.getFile());
...
FilegetFile(URL ressource)
get File
StringBuilder file = new StringBuilder();
String host = ressource.getHost(), path = ressource.getPath();
if (host != null && !host.isEmpty())
    file.append(File.separatorChar).append(File.separatorChar).append(host);
return new File(file.append(path.split("!", 2)[0]).toString());
FilegetFile(URL url)
For local file conversion.
File f = null;
try {
    f = new File(url.toURI());
} catch (URISyntaxException e) {
    f = new File(url.getPath());
return f;
StringgetFile(URL url)
get File
String path = url.getPath();
int index = path.lastIndexOf('/');
String file = index == -1 ? path : path.substring(index + 1);
return file;
FilegetFile(URL url)
get File
return new File(getFilePath(url));