Java URL to File Name getFile(URL resourceUrl, String description)

Here you can find the source of getFile(URL resourceUrl, String description)

Description

get File

License

Apache License

Declaration

public static File getFile(URL resourceUrl, String description) throws FileNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    public static final String URL_PROTOCOL_FILE = "file";

    public static File getFile(URL resourceUrl) throws FileNotFoundException {
        return getFile(resourceUrl, "URL");
    }/*from   ww w . j a va2s  . c  o  m*/

    public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
        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 {
            return new File(toURI(resourceUrl).getSchemeSpecificPart());
        }

        catch (URISyntaxException ex) {
            // Fallback for URLs that are not valid URIs (should hardly ever happen).
            return new File(resourceUrl.getFile());
        }
    }

    public static URI toURI(URL url) throws URISyntaxException {
        return toURI(url.toString());
    }

    public static URI toURI(String location) throws URISyntaxException {
        return new URI(location.replace(" ", "%20"));
    }
}

Related

  1. getFile(URL aURL)
  2. getFile(URL resourceUrl, String description)
  3. getFile(URL resourceUrl, String description)
  4. getFile(URL resourceUrl, String description)
  5. getFile(URL resourceUrl, String description)
  6. getFile(URL ressource)
  7. getFile(URL url)
  8. getFile(URL url)