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

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source 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, String description) throws FileNotFoundException {
        if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
            throw new FileNotFoundException(description + "->" + resourceUrl);
        }//from   w  w w .  ja v  a 2s. c  o m
        try {
            return new File(toURI(resourceUrl).getSchemeSpecificPart());
        } catch (URISyntaxException ex) {
            return new File(resourceUrl.getFile());
        }
    }

    public static File getFile(URI resourceUri, String description) throws FileNotFoundException {
        if (!URL_PROTOCOL_FILE.equals(resourceUri.getScheme())) {
            throw new FileNotFoundException(description + "->" + resourceUri);
        }
        return new File(resourceUri.getSchemeSpecificPart());
    }

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

    public static URI toURI(String location) throws URISyntaxException {
        return new URI(location.replaceAll(" ", "%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)