Java URI to toFile(URI uri)

Here you can find the source of toFile(URI uri)

Description

Returns the URI as a local file, or null if the given URI does not represent a local file.

License

Open Source License

Parameter

Parameter Description
uri The URI to return the file for

Return

The local file corresponding to the given URI, or null

Declaration

public static File toFile(URI uri) 

Method Source Code


//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.File;

import java.net.URI;

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

    /**// w w w  .j ava  2 s  .  c o m
     * Returns the URI as a local file, or <code>null</code> if the given URI does not represent a local file.
     * 
     * @param uri The URI to return the file for
     * @return The local file corresponding to the given URI, or <code>null</code>
     */
    public static File toFile(URI uri) {
        try {
            if (!SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) {
                return null;
            }
            // assume all illegal characters have been properly encoded, so use URI class to unencode
            return new File(uri);
        } catch (IllegalArgumentException e) {
            // File constructor does not support non-hierarchical URI
            String path = uri.getPath();
            // path is null for non-hierarchical URI such as file:c:/tmp
            if (path == null) {
                path = uri.getSchemeSpecificPart();
            }
            return new File(path);
        }
    }
}

Related

  1. toExternalForm(URI u)
  2. toFile(java.net.URI uri)
  3. toFile(String filenameOrFileURI)
  4. toFile(String unixPathOrUri)
  5. toFile(URI uri)
  6. toFileURI(File file)
  7. toFileURI(String path)
  8. tokenizeBase(URI uri)
  9. toName(URI uri)