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

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

Description

Resolve the given resource URL to a java.io.File, i.e.

License

LGPL

Parameter

Parameter Description
resourceUrl the resource URL to resolve
description a description of the original resource that the URL was created for (for example, a class path location)

Exception

Parameter Description
FileNotFoundException if the URL cannot be resolved to a file in the file system

Return

a corresponding File object

Declaration

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

Method Source Code

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

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /** Pseudo URL prefix for loading from the class path: "classpath:" */
    public static final String CLASSPATH_URL_PREFIX = "classpath:";
    /** URL protocol for a file in the file system: "file" */
    public static final String URL_PROTOCOL_FILE = "file";

    /**/*  w ww  . j  a v a2s.co  m*/
     * Resolve the given resource location to a <code>java.io.File</code>, i.e.
     * to a file in the file system.
     * <p>
     * Does not check whether the file actually exists; simply returns the File
     * that the given location would correspond to.
     * 
     * @param resourceLocation
     *            the resource location to resolve: either a "classpath:" pseudo
     *            URL, a "file:" URL, or a plain file path eg,
     *            "classpath:config/log4j.default", "file:/C:/reinier.reg",
     *            "C:/reinier.reg"
     * @return a corresponding File object
     * @throws FileNotFoundException
     *             if the resource cannot be resolved to a file in the file
     *             system
     */
    public static File getFile(String resourceLocation) throws FileNotFoundException {
        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
            String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
            String description = "class path resource [" + path + "]";
            URL url = Thread.currentThread().getContextClassLoader().getResource(path);
            return getFile(url, description);
        }
        try {
            // try URL
            return getFile(new URL(resourceLocation), "URL");
        } catch (MalformedURLException ex) {
            // no URL -> treat as file path
            return new File(resourceLocation);
        }
    }

    /**
     * Resolve the given resource URL to a <code>java.io.File</code>, i.e. to a
     * file in the file system.
     * 
     * @param resourceUrl
     *            the resource URL to resolve
     * @param description
     *            a description of the original resource that the URL was
     *            created for (for example, a class path location)
     * @return a corresponding File object
     * @throws FileNotFoundException
     *             if the URL cannot be resolved to a file in the file system
     */
    private static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
        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) {
            // Fallback for URLs that are not valid URIs (should hardly ever
            // happen).
            return new File(resourceUrl.getFile());
        }
    }

    public static File getFile(String resourceLocation, boolean allowedcreate) throws IOException {
        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
            String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
            URL url = Thread.currentThread().getContextClassLoader().getResource("");
            resourceLocation = url.getPath() + path;

        }
        File file = new File(resourceLocation);
        if (allowedcreate && !file.exists()) {
            file.createNewFile();
        }
        return file;
    }
}

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)